Member • Jul 18, 2014
How are int main() and void main() different?
Many new C/C++ programmers get confused between the proper use of int main()
and void main()
functions. In this article we will understand these functions and look at sample code.
int main()
: This is the most standard way to write the main function in both C and C++. When a program finishes execution, it is customary for it to return a status code to the operating system.int main()
signifies that the main function should return an integer value to the operating system. This value is usually 0, which means the program has successfully executed, but can be another value to signify an error. The exact meaning of non-zero return codes can depend on the program.void main()
: This function doesn't return a value. It is considered non-standard in both C and C++, even though some compilers may allow it.
The C++ standard states that the main function should return an int, so int main()
is the correct and standard way to define the main function in C++.
If you do not explicitly return a value from int main()
, C++ automatically adds return 0;
at the end for you.
In C, while void main()
may be allowed by some compilers, it is not portable or standard-compliant.
The C standard also says that the main function should return an int. In practice, this means that while void main()
might work on your compiler, it might not work on another.
int main() code example
Take a look at following C++ program-
#include
int main() {
printf("Hello, world!\n");
return 0;
}
In this case, return 0;
signifies that the program has finished execution successfully. If we don't include return 0;
, the C++ compiler will implicitly add it, but in C, it's better to add it explicitly.
Including conio.h in code
Regarding #include
, this is a header file used in old MS-DOS compilers to provide console input/output.
It's not standard, and it's not necessary or recommended for modern programming in C or C++. It's not related to the int main()
function and shouldn't typically be used in portable code. Instead, for console input and output in a portable way, you should use stdio.h
in C or iostream
in C++.
Common Mistakes New C/C++ Programmers Make
A common mistake when beginners start learning C or C++ is to forget to return a value from int main()
.
In C++, this isn't a problem because the compiler will add return 0;
implicitly.
However, in C, not returning a value when one is expected results in undefined behavior, which could lead to any result.
Another mistake is to use void main()
because they've seen it in some non-standard code or outdated textbooks.
It's important to understand that void main()
is not standard-compliant, and using it can lead to portability problems.
Summary
To summarize, you should always use int main()
in C and C++, and you should return a value from main in C. Avoid using void main()
because it's not standard.
The use of #include
is unrelated to int main()
, and this header is not recommended for modern, portable code.