#include <stdio.h>
void main()
{
printf("hi");
}


.....................

#include <stdio.h>
int main ( void)
{
printf("hi");

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?

Recommended Answers

All 6 Replies

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.

commented: An awful lot of misinformation +0

...
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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.