Why does my compiler tell me that Return 0 is undeclared? I've never seen this before.Code:main()
{
int sum;
/* Compute result */
sum = 25 + 37 - 19;
/* Display Results */
printf("The answer is %i\n",sum);
Return 0;
}
Printable View
Why does my compiler tell me that Return 0 is undeclared? I've never seen this before.Code:main()
{
int sum;
/* Compute result */
sum = 25 + 37 - 19;
/* Display Results */
printf("The answer is %i\n",sum);
Return 0;
}
C is case sensitive. 'Return' is not the same as 'return'.
What WoodSTokk wrote is indeed true.
Also - int main() ;)
True, int main(void) - it is C after all. My bad.
Since C99 you can safely omit return 0; - and I'd argue that EXIT_SUCCESS is equivalent, but is it strictly better?
I mean, sure, the issue is it's a magic number - and a named constant is a better way, but it's so widely known magic number - and is used by the standard itself - that by this point there's very little magic to it. ;)Quote:
5.1.2.2.3 Program termination
1 If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument; reaching the } that terminates the main function returns a value of 0.
7.22.4.4 The exit function
5 [...] If the value of status is zero or EXIT_SUCCESS, an implementation-defined form of the status successful termination is returned.
Out of curiosity, that quote from 7.22.4.4. applies also to the exit function being called from within a function other than main?
Yes, since the "exit function causes normal program termination to occur", without regard to which function it is called from.
It doesn't address the issue directly [edit - I may have pulled the trigger too early, indeed, as laserlight says:]
thus the only interpretation that makes sense is that exit() works as is laid out when called, no matter where it is called from. (Remembering that part of the rules is that you cannot call it twice, or call exit() and quick_exit(), since that's UB. So there shall be no calls to exit() in functions registered using atexit() or at_quick_exit().)Quote:
The exit function causes normal program termination to occur.
> I'd argue that EXIT_SUCCESS is equivalent, but is it strictly better?
It's more of a programming style thing than any objective "betterness". I'd favor consistency though; if you use EXIT_FAILURE anywhere, then favor EXIT_SUCCESS as well. Otherwise, returning 0 from main is so pervasively understood that being a magic number is somewhat of a non-issue.
It is more than that. There are some O/Ss that do not or may not use a return value of 0 to indicate success, and a specific non-zero value to indicate failure. Using the constants insure the code will work correctly no matter what O/S it is compiled for.
Also, it is more self documenting to use the constants.
I wish everyone were using compilers that adhere to C99 or C11, but unfortunately, not the case.
It is amazing how many people in the world are learning C on Turbo-C, hopefully NOT version 1.0! I am currently using gcc version 6.2.0, and use C11 by default, and I still prefer to use "return EXIT_SUCCESS;" from all my main functions, plus anyone I am teaching or advising, along with "int main(void)" for any program not using command line arguments.
If you wish to omit it, then fine, but since many of the readers of this forum are learning C, I prefer to be more thorough.
From the C99 draft standard.Quote:
There are some O/Ss that do not or may not use a return value of 0 to indicate success,
So both EXIT_SUCCESS and zero indicate the same thing as far as the C standard is concerned. EXIT_FAILURE is a different matter, returning anything other that EXIT_FAILURE may not work properly. For example even though main() is defined to return an int and exit() has an int parameter some operating systems only allow values that can be represented by a signed char.Quote:
7.22.4.4 The exit function
Synopsis
#include <stdlib.h>
_Noreturn void exit(int status);
Description
...
5 Finally, control is returned to the host environment. If the value of status is zero or
EXIT_SUCCESS, an implementation-defined form of the status successful termination is
returned. If the value of status is EXIT_FAILURE, an implementation-defined form
of the status unsuccessful termination is returned. Otherwise the status returned is
implementation-defined.
The important part to notice is that even though you return 0 from main (either implicitly, or explicitly) the successful termination status that the actual compiled program returns is implementation-defined. It may be that on some OS successful termination status value is 1, and exactly that will be returned from the program whether 0 or EXIT_SUCCESS will be used as an operand of return in main() or passed to exit().
Again, as long as the compiler is standard conforming, 0 is always a safe value for successful termination, no matter what value a particular OS uses.
The discussion is interesting for another (ironic) reason. As we come to discuss the standard defined behaviour, we come full circle to post #4 and should realize by now none of that is really necessary. :)
On many cases, being thorough doesn't convey a special level of correctness. The advise on post #3 is enough. The void argument isn't really required and even returning 0 (whether from the return statement or exit function) at the end of a main function is an exercise in futility since the C standard dictates the compiler should already do that for us. (We actually didn't discuss that particular, but it was on everyone's mind).
In theory, the void parameter may be required by some later version of the C standard since:Quote:
Originally Posted by Mario F.
My reasoning is that the void parameter would fall under "prototype-format parameter type declarators", whereas an empty parameter list would actually be an empty identifier list, and hence is obsolescent under 6.11.7. In practice I think they will retain this status for a long while since there's some convenience for allowing what C++ allows (and in fact typically C++ programmers prefer not to use void) to cater to programs intended to be compiled as C and C++ as the case may be.Quote:
Originally Posted by C11