Q: What's the correct declaration of main()?
A: There are two valid declarations:
although they can be written in a variety of ways. The second parameter may be declared char *argv[] (see question 6.4), you can use any names for the two parameters, and you can use old-style syntax:Code: Select all
int main(void) int main(int argc, char **argv)
Code: Select all
int main() int main(argc, argv) int argc; char **argv;
Trenutni draft budućeg c1x standarda dozvoljava sledeće dve definicije:Q: Can I declare main as void, to shut off these annoying ``main returns no value'' messages?
A: No. main must be declared as returning an int, and as taking either zero or two arguments, of the appropriate types. If you're calling exit() but still getting warnings, you may have to insert a redundant return statement (or use some kind of ``not reached'' directive, if available).
Declaring a function as void does not merely shut off or rearrange warnings: it may also result in a different function call/return sequence, incompatible with what the caller (in main's case, the C run-time startup code) expects. That is, if the calling sequences for void- and int-valued functions differ, the startup code is going to be calling main using specifically the int-valued conventions, and if main has been improperly declared as void, it may not work. (See also question 2.18.)
Code: Select all
int main(void){ /* .... */ }
int main(int argc, char *argv[]){ /* ... */}
A ovaj tip definicije je bio praksa u pred-standardnom C-u (k&r):
Code: Select all
main(){ ... }
Code: Select all
main(argc,argv)
int argc, char **argv;
{ ...}
http://c-faq.com/ansi/index.html
http://www.open-std.org/jtc1/sc22/wg14/ ... /n1548.pdf