Difference between void main and int main in C/C++?

73,117

Solution 1

The difference is one is the correct way to define main, and the other is not.

And yes, it does matter. Either

int main(int argc, char** argv)

or

int main()

are the proper definition of your main per the C++ spec.

void main(int argc, char** argv)

is not and was, IIRC, a perversity that came with older Microsoft's C++ compilers.

https://isocpp.org/wiki/faq/newbie#main-returns-int

Solution 2

Bjarne Stroustrup made this quite clear:

The definition void main() is not and never has been C++, nor has it even been C.

See reference.

Solution 3

You should use int main. Both the C and C++ standards specify that main should return a value.

Solution 4

For C++, only int is allowed. For C, C99 says only int is allowed. The prior standard allowed for a void return.

In short, always int.

Solution 5

The point is, C programs (and C++ the same) always (should?) return a success value or error code, so they should be declared that way.

Share:
73,117
vishwas kumar
Author by

vishwas kumar

Bad programming is easy. Idiots can learn it in 21 days, even if they are dummies. --Teach Yourself Programming In Ten Years

Updated on July 09, 2022

Comments

  • vishwas kumar
    vishwas kumar almost 2 years

    Does it matter which way I declare the main function in a C++ (or C) program?