Asked By
anonymous
7060 points
N/A
Posted on - 09/20/2011
Hi,
Â
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 ?
Thanks
Â
#include <iostream>
using namespace std;
Â
void main() //instead of "int main()"
{
 cout<<"Hello world"
Â
 //return 0;
}
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.
Â