Interview Questions

What is the right declaration for main? Is void main() correct?

C Interview Questions and Answers


(Continued from previous question...)

What is the right declaration for main? Is void main() correct?

But no, it's not correct.

There are two valid declarations:
int main(void)
int main(int argc, char **argv)
although they can be written in a variety of ways. The second parameter may be declared char *argv[], you can use any names for the two parameters, and you can use old-style syntax:
int main()
int main(argc, argv)
int argc; char **argv;


Perhaps its author counts himself among the target audience. Many books unaccountably use void main() in examples, and assert that it's correct. They're wrong, or they're assuming that everyone writes code for systems where it happens to work.

(Continued on next question...)

Other Interview Questions