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.
Replies
-
Deepika BansalDifference betwèen int main and void main: See like any other function, main is also a function but with a special characteristic that the program execution always start from main. So the function main needs arguments and a return type. These int and void are its return type. Void means it will not return any value, which is also ok.
But if want to know whether the program has terminated successfully or not, we need a return value which can be zero or a non zero value. Hence the functuon becomes int main () and is recommended over vpid main ().
Hope its helps you. -
Rahul NaraniyaThe same thing we can do by using getch() (for void main). then why int main is suggested?? is there any advantage of int main over void main??
-
beingabthe standard statements are:
int main(void)
int main(int argc, char **argv)
You can also use main() simply that means same as int main()..
int main() returns an exit value to compiler and works on most compilers.
And getch() as you mentioned has nothing to do with it. It gets character input on screen or else holds screen in other words -
Rahul Naraniyaohk... got it thanks... 😀
-
ravirajputgoood yr...
-
SK Jindal
void implies no return type and int main means that main() will return an integerRahul Naraniyacan anyone explain what is difference between int main and void main??? why it is suggested to use int main instead of void main?? and why we need [HASHTAG]#include[/HASHTAG]when we use int main??? -
kothandaraman
#include
now see the guys when you use the function having some parameters than time return some a and b values so now use int main functionvoid main() { clrscr(); add(5,4); getch(); } int add(int a,int b) { int c; c=a+b; printf("Output: %d",c); return 0; }
if use void add(int a,int b) system throw the error. so i use int add(int a,int b) -
kothandaramanabove the program any doubt message me
-
rahul691. int main() is preferred over void main(), as per standards, and most new compilers supports int main() rather than void main().
2. int main() can help to return the error code if your program fails due to some error.
(Pre exception handling days, now you may use exception handling to handle issues,
perhaps that's the reason in java we go as public static void main () )
😀 -
Gowtham369369i have replaced void main() with int main() there is same result then what is the difference......?
-
Walter BoydThe difference is, in C, int main() can be called with any number of arguments, but int main(void)can only be called without any argument. Although it doesn’t make any difference most of the times, using “int main(void)” is a recommended practice in C.
-
Gowtham369369Okay! then I will use only int main() so that there will be no confusion for me....
-
Base Developer
How can I came to know that program is returning a value after using int main function
-
GEETHA NANI
can anyone suggest me a video of difference between this void main and int main
-
GEETHA NANI
can anyone suggest me a video of difference between this void main and int main
-
Ankit Anand
The above program has compilation error because we must define the add method before main method
-
AdhikariHarika Reshma
#-Link-Snipped-#
#-Link-Snipped-#Â hope the above link for the difference between INT main and void main help you.
You are reading an archived discussion.