Differences between int main() and void main() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 9

Differences between int main() and void main()

What is the most efficient way to define the main() function in C++ : int main() or void main() and why? I used to write void main() for then not to write return 0;.. is there any difference except the return type?

29th Oct 2016, 11:26 AM
Rebeka Asryan
Rebeka Asryan - avatar
4 Answers
+ 12
yes there are differences, as developers of C++ said there is no such a thing as void main(). It's smth created by Microsoft and only Microsoft's compiler can handle that. Every other compiler (e.g. gcc) are working only with int main(). So, in my opinion it's good practice to only use int main() in order to avoid conflicts with other compilers.
29th Oct 2016, 12:35 PM
Aaron Sarkissian
Aaron Sarkissian - avatar
+ 3
This question already appeared today, so here is just my copied answer: Void means something line "no return value", while int has to return integer. It is always bad to use void main(), because only some compilers support it, so it's possible you will get an error just because you use void main(). When you use int main(), it needs to return a value (return 0 means "program executed successfuly"). In some compilers return 0 is not necessarry, because they will add it to the end if you didn't do that. So always use 'int main()' with 'return 0' statement in the end.
29th Oct 2016, 12:14 PM
Daniel Oravec
Daniel Oravec - avatar
+ 2
int main means that the main returns an integer value as while using function we use void that implies that the function does not return any value similar is the case with main.
31st Oct 2016, 1:18 PM
MSK
MSK - avatar
0
simple answer: int returns a value void doesn't return a value
2nd Nov 2016, 7:43 PM
Péter Subecz
Péter Subecz - avatar