What is the difference between void main and int main? | Sololearn: Learn to code for FREE!
Novo curso! Todo programador deveria aprender IA generativa!
Experimente uma aula grƔtis
+ 5

What is the difference between void main and int main?

27th Sep 2016, 12:40 AM
Nathan
Nathan - avatar
7 Respostas
+ 24
int mainĀ means you will end your program withĀ return 0;Ā (0 is the standard for everything went successfully). While void mainĀ will allow you to skip writing "return 0;", as void functions don't have to return anything. Anyway I recommend you to use int main(), as using void main() can cause some problems in many compilers. To avoid such issues just write int main() and at the end "return 0;" :)
15th Nov 2016, 6:06 AM
LyannaM
LyannaM - avatar
+ 3
void main will return nothing to the caller function. But for the int main you have to return integer value to the caller function. void main() { //do something } int main() { //do something return 0; }
27th Sep 2016, 1:34 AM
mahesh kumar
mahesh kumar - avatar
+ 2
In addition to what @LynnaM said. You only use void in a method where you don't need any returning data from that method. But as you may know, in c++ everything get executed in the main method for that reason it's always recomendable to give a return to the main. And that return should be 0, means everything went fine cause if something went wrong in your main the compiler will return a value different than 0. If you use void in your main, and when executing your program an error occurs the compiler will complain and you won't get a much help. To the point, you should always use int main in c++.
15th Nov 2016, 9:44 AM
korka25
korka25 - avatar
+ 2
If you look into the ghost, the.system down to the cpu, you will see only functions with a return mechanism. But sometimes there is no need to use it. If your program has no return value to use, you can use "void" to make it clear.
7th May 2017, 11:29 AM
Ernst Belar
Ernst Belar - avatar
+ 1
Difference is that you should always use int main and never use void main:: int main() and int main(int args, char * argv[]) are part of C++ standard and void main() is an extension that is compiler specific and may or may not work. int main(){} is also special since not returning from it is same as int main(){return 0;} which is probably the same what void main(){} returns (yes these special rules don't apply to any other function than main so it's probably a bad practice to use them since it confuses beginners - prefer generality in code and never make special cases if they don't make a singificant impact that is greater than the confusion caused by having to learn more :)).
17th Nov 2016, 8:46 PM
Domen Vrankar
Domen Vrankar - avatar
+ 1
just remember that when using void this means that no value it's returned
28th Dec 2016, 12:27 AM
Emil Tsvetanov
Emil Tsvetanov - avatar
0
But how do people can execute a code with void?
20th Dec 2016, 8:14 PM
Nathan
Nathan - avatar