Int main () and void main, what is the difference ?

Asked By 7060 points N/A Posted on -



I'm a beginner at coding, I know that int main () is where the program starts but on some websites I found instead of using int main () ( and return 0; before the last bracket), it was void main().


Is there any difference ? And does it affect the compiling speed ?



#include <iostream>

using namespace std;
void main() //instead of "int main()"
 cout<<"Hello world"
 //return 0;
Answered By 0 points N/A #80689

Int main () and void main, what is the difference ?


Hi, Abdelaaty!

Aside from the fact that void main() and int main() are different in their returning types, there is a big difference when it comes to comparing the two.

First, according to the standard, only these two syntax are acceptable for use:

int main ( int argc, char *argv[] )
int main ()

Therefore, it just means that using void main() is somewhat wrong.

Second, C++  does require a return value to indicate failure or success of a program. Using and declaring void main() doesn't mean that the program you are creating is not returning any value. It simply means that you could have a returned value that is not 0. Experiencing this situation may lead to hours of seeking for a bug on your program, thus compiling your program becomes tedious.

In the case of int main(), it uses the return 0 statement. Having a returned value of 0 (zero) means that the program that you have created has been properly terminated.

Hopefully, this answer helps you.


Login/Register to Answer

Related Questions