Showing posts with label calling main. Show all posts
Showing posts with label calling main. Show all posts

Monday, February 2, 2009

Should I use void main() or int main() or char main() or what?

Well,
I guess this the the most popular newbie question in C++.
The straight answer lies here:
Use int main()
int main() and nothing else but int main();
or if you need command line arguments use:
int main(int argc,char** argv);
Actually, there is no thing as void main() in C++ and even in C.
If your compiler is allowing you to use void main(), probably its a bad bad compiler and you should switch to a standard compiler which follows the standardization of C++.
Remember, that main() can return only int. And it has to return a int. Its return type is only int.
If you are not getting this point straight, most probably what is need is a explanation:
The real explanation is that it is a standard decided by ISO C,C++ Standardization Committee. Yes it is a standard and one should follow it strictly.
The other reason can be given to satisfy you and your soul.
One of them is that the Operating System uses the return value for various other purposes. Now don't say the my specific operating system(most of you would talk about Microsoft's operating systems such as windows XP) can run program which returns void. If your operating system can, it doesn't means that all operating systems can( Don't feel proud that your operating system can run those program. Its not a good thing that they does.). To give an example, most of the Unix and Linux operating system often uses the return value to ensure that execution of the program has been done correctly.

Few point I would like to add while discussing about return type of main() as per the standards:
Unlike other functions, main() doesn't assume the default return type to be int. That means, you cannot skip writing the int before the definition of main() and assume that the return type is int.
#include
main()//wrong, has to be int main()
{

return(0);
}


You have to explicitly write int main(){} and define main() like this
#include
int main()
{



return (0);
}

Secondly, you may skip the return(0); statement while ending the main() like this
#include
int main()
{
cout<<"Hello World";

}

as the example above, I have not issued a return(0); statement. Hence, by default a return(0) has been issued for you by the compiler itself.


Third, issue is not related to return type of main() but actually related to main() itself.
I have seen many programs calling main() from other functions. Like:
.
.
.
void func1();
int main()
{

//some code here too
func1();
//more code here
return(0);
}

void func1()
{
//do something
//do something
main();
}

I still can't understand why someone would like to call main().
Remember, if you are calling main() you are not a good programmer and doesn't care about the program flow and utilization of resources. Here is what I have to say:
Do whatever but don't call main() ever. main() is to be called only by the system once your program executes. You should never ( and will never need to) call main().
If you call main(), eventually your program will break down due to lack of resources.
Don't write to me telling that it works. Its a illusion that it works. It is actually degrading your programing skills as well as the resources of the system you are running your program at.

To conclude, I just want to say that if you stick with the Standards, you will never fall away from right coding.
And remember that there is no void main() or char main() but just int main().
Also that you should never call main() in any of your functions including from main() itself.
Thank You.