Possible Duplicate:
What is the proper declaration of main?
I am working on my C skills and I have noticed that
int main( int argc, char *argv[] )
and
return (EXIT_SUCCESS)
instead of
int main() and return 0
Why is this?
Possible Duplicate:
What is the proper declaration of main?
I am working on my C skills and I have noticed that
int main( int argc, char *argv[] )
and
return (EXIT_SUCCESS)
instead of
int main() and return 0
Why is this?
int main (int argc, char * argv [])
is for when you want to take command line arguments.EXIT_SUCCESS
is just a#define
that's more descriptive than0
.Operating Systems can differ in how a program indicates successful operation. The macro
EXIT_SUCCESS
ideally expands to a value appropriate for the system the program is compiled for.http://en.wikipedia.org/wiki/Exit_status
(The two things you ask have nothing to do with each other.)
To answer your first question: Having int main() just means that the program is not accepting command line arguments. When it takes the two arguments, argc is the argument count (it will always be greater than or equal to one, since the first argument will be the path or name of the program itself), and argv is the list of arguments.
To answer your second question: EXIT_SUCCESS is guaranteed to be interpreted as success by the underlying operating system.
If you are going to ignore the argument list, it is reasonable and sensible to use:
The standards bless this usage, as well as the one with arguments. You get a warning from GCC if you compile with
-Wstrict-prototypes
and don't include thevoid
, so I write thevoid
. C++ is different here.As for
return EXIT_SUCCESS;
, there seems to be little benefit to it in general; I continue to writereturn 0;
at the end of amain()
function, even though C99 permits you to omit any return there (and it then behaves as if you wrotereturn 0;
).Aside: Note that §5.1.2.2.3 clearly indicates that the C standard allows an implementation to permit return types for
main()
other thanint
(unlike the C++ standard, which expressly forbids a return type other thanint
). However, as Jens rightly points out, a non-int
return type frommain
is only allowed if the implementation explicitly documents that it is allowed, and the documentation will probably place limits on what is allowed.int main( int argc, char *argv[] )
allows the user input arguments at the execution of the program, i.e., that text you write in the console after the program name.return (EXIT_SUCCESS)
is in case an O.S. doesn't expect 0 as a value of successful exit: it would be any other value, but in most cases, EXIT_SUCCESS equals 0.