#include <stdio.h>
void main()


#include <stdio.h>
int main ( void)

return 0;


q1: What is the diff in two codes?
q2: I am habitual of using code type 1 ( the first code), is it ok to follow that way?

6 Years
Discussion Span
Last Post by gudads

The difference, quite obviously, is the definition of main .

In the first version, the function is defined as void main() . Therefore, it does not return anything. Furthermore, it takes an infinite number of arguments.

The second version has the function defined as int main(void) . It returns an int value and takes no arguments. The return statement, return 0; , informs the operating system that the program has ended with no errors.

The first version is considered to be bad practice, so you should get in the habit of using the second version.


If you read about function in c/c++, you must notice that function have three thing namely
1.Its return type
2.Function Name
3.List of parameter

syntax: return-type function-name(list-of-parameter)

and main() is also function in c/c++ , and must have following three thing.
But in our program we just declare the body of main() function.
Now come to your problem
In the first version, the function is defined as void main(), which means it doesn't return any thing and doesn't take any arguments.
Second one return one integer value and take no argument.
Both are correct and have no problem at all.
Best of Luck.

Votes + Comments
An awful lot of misinformation

Both are correct and have no problem at all.

For a hosted environment, only the second version is explicitly given in the C99 standard. The first is only valid if it is covered under the "or in some other implementation-defined manner." clause.


thanx.. but two different books follow two different programming style..

i do understood the difference..

but why two different writes follow two different styles...
whereas software programmers recommend using code type 2.

int main(void)


Naming the books would (have) be(en) extremely useful. My guess is that the void main() book is old and outdated.


thanks.. i will follow

int main ( void )

now on.. for the sake of good programming style.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.