The motivation behind writing this article came from the fact that there are still many books/tutorials/softwares etc where I have seen 'void main' being used instead of 'int main'. Still most of the C/C++ newbies are taught to start practice coding with 'void main'. Even I started practicing C in high school with 'void main' but I think that was some 10 yrs back and was mostly due to lack of awareness. But, now tutors/mentors/teachers should understand the fact that using 'void main()' instead of 'int main()' is a wrong practice.
Here in this short article, I'll take a small example to let you know why its a wrong practice to use void main().
Well, there could be three quick answers to this question :
"I don't know but some how it works for me and as long as it works I don't care about all this".
Well, they are actually correct. Unless they get to see any problem by using void main(), why do they think over it?
Lets take the point(3) above and try to prove that its actually dangerous to use void main() instead of int main().
The proof requires :
Well, after a bit of thought It came to my mind that we can actually take a dummy function 'func()' in our program that is defined in a separate file while declared and called from a separate file. So the scenario will be set just as in case of the function main().
Fine, looks good enough. Lets look at the code :
In the above code, treat func() as if its was main().
We have declared function func() as extern.
NOTE : This file can be thought of as a file that contains startup routine that calls main().
The code above is where we have actually defined func(). Take a look, we declared func() as returning int earlier but defined here as returning void.
NOTE: This file can be thought of as any normal file where we define main() as returning void().
Also, we have the above header.
Compile the above code using :
Also, lets run the program :
Wohhooo....A magical value 16 is returned by the function func(). Where does this value came from? Well, it could well be garbage in 'ret' as this variable does not receive any value from the call to func().
Now, lets see the script that uses the executable prepared out of all the above code :
The script above runs the executable and then checks the value returned by the executable to the environment.
Note that the return value of the function func() is used as the final return value to the environment. Since the definition of func() returns nothing, so lets see what happens when the script is run :
EXPECTED!!!...The script has gone into the failure logic based on the program's return value despite of the fact that our program executed successfully.
Well folks, this is what could happen if we will use the return type of main() as void.
To conclude, this article contains a practical example of why main() should not be declared as void. Hope this helps those people understand the concept those who are still using void main().
Stay tuned for more!!!
Here in this short article, I'll take a small example to let you know why its a wrong practice to use void main().
Whats wrong in it?
Well, there could be three quick answers to this question :
- The ANSI standard says that the main() function should be declared with return type 'int'.
- The start-up functions that call main() do expect that the return value of main() be pushed onto stack. Which if not done may cause problems like stack corruption in the programs exit sequence.
- Returning no value to the invocation environment that expects a return value may cause some weird problems especially when you run your program from some script that checks the return vale of your program
"I don't know but some how it works for me and as long as it works I don't care about all this".
Well, they are actually correct. Unless they get to see any problem by using void main(), why do they think over it?
Lets prove it
Lets take the point(3) above and try to prove that its actually dangerous to use void main() instead of int main().
The proof requires :
- The code that defines main() [easy, as every program has main defined]
- The code that calls main() [difficult, as this code is usually hidden]
- A shell script that calls our program [not difficult]
Well, after a bit of thought It came to my mind that we can actually take a dummy function 'func()' in our program that is defined in a separate file while declared and called from a separate file. So the scenario will be set just as in case of the function main().
Fine, looks good enough. Lets look at the code :
Code:
#ifndef TEST_H_INCLUDED
#include"test.h"
#endif
extern int func();
int main(void)
{
int ret = func();
printf("\n Executed!!! with ret[%d] \n", ret);
return ret;
}
We have declared function func() as extern.
NOTE : This file can be thought of as a file that contains startup routine that calls main().
Code:
#ifndef TEST_H_INCLUDED
#include"test.h"
#endif
void func()
{
printf("\n Inside func()\n");
}
NOTE: This file can be thought of as any normal file where we define main() as returning void().
Code:
#define TEST_H_INCLUDED #include<stdio.h>
Compile the above code using :
Code:
gcc -Wall test.c test1.c -o test
Code:
~/practice $ ./test Inside func() Executed!!! with ret[16]
Now, lets see the script that uses the executable prepared out of all the above code :
Code:
#!/bin/bash
./test
if [ $? -eq 0 ] ; then
echo "SCRIPT :: Program ran successfully"
else
echo "SCRIPT :: Program failure"
fi
Note that the return value of the function func() is used as the final return value to the environment. Since the definition of func() returns nothing, so lets see what happens when the script is run :
Code:
$ ./script.sh Inside func() Executed!!! with ret[16] SCRIPT :: Program failure
Well folks, this is what could happen if we will use the return type of main() as void.
Conclusion
To conclude, this article contains a practical example of why main() should not be declared as void. Hope this helps those people understand the concept those who are still using void main().
Stay tuned for more!!!


