PDA

View Full Version : What is difference among main() int main() void main()



saneha
05-02-2011, 05:51 PM
What is difference among

main() int main() void main()

and where there are used in C or C++

Vuhelper
05-02-2011, 05:51 PM
In C and C++, int main() is proper code, because main() always returns int according to the latest specifications for C and C++.

int main() with no arguments is interpreted as int main(void). Earlier versions of the spec and pre-standard versions of C required explicit declaration [ie. int main(void)] but the current specs for both languages allow for implicit declaration. I believe even main() would be acceptable, though not formal enough for some people.

void main (void) is not proper code, because main() always returns int. That's just how it's defined by the specification. Most compilers will accept it with just a warning, and interpret it as int main(void) before compiling. It's not likely to cause any problems as you can be fairly (but not 100%) confident that the compiler will fix it for you. You will find some books that use void main(void), but it's considered incorrect, or at least sloppy.

main(int argc, char *argv[])

argc is the number of argv. In one sense.
argc is the number of arguments like -b,-ab . In other sense.

argv is the argument . like -b , -ab ..., but first argv[0]=filename with full path