+ 5
What is the difference between void main and int main?
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;" :)
+ 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;
}
+ 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++.
+ 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.
+ 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 :)).
+ 1
just remember that when using void this means that no value it's returned
0
But how do people can execute a code with void?