Tag Archives: int main v/s void main

int main v/s void main

what should be the return type of main?

Someone say “it’s void” and others say “int main”.

In C++ the default return type of main function is int; ie if you put simply “main”,it will be int main()
And in C no default type is set;

one of the classic mistakes that made by C/C++ programmers is the use of void main.
you may be the argument that “it works in my system”. Even if void main works in your system,
it may generate errors if the same code is compiled with some other compilers.

return type of a function void means, that function returns nothing; by making main function
as void main() we restrict it from returning a value.
As we know operating system(OS) is a set of programs; while executing our C/C++  code
the OS itself call our program and after execution our program returns the control to OS.

if our program code executed successfully , it returns 0(zero)
and returns a nonzero value, if not executed successfully.
so we should use int main; then only our program can return a value and os knows it’s status, whether the program executed completely or not.

While using int main , it’s expected to put a ”  return 0;  ” statement at the end , else compiler wiil generate a warning “function should return a value”.
so you can use a syntax similiar to this.

int main()
{

/*body*/

return 0;
}