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.

Sunday, October 12, 2008

Why you should'nt use you use a Old C++ Comiler

Long time back, I remember when I was learning C++, I use to work on a DOS based compiler called Turbo C++ 3.0 while was a IDE(Integrated Development Environment).
Every thing was fine until I discovered that the statement :
using namespace std;
did not had any meaning for my compiler. This perhaps was very suprizing for me as by my text book at that time (Object Oriented Programing in C++ by Robert Lafore), this was to be include it in every listings.

Actualy, namespaces was introduced in 1996. They categorized the use of a particular type of clasess and objects in different namespaces. The std namespace (std => standard) comprises of several objects as cout, cin and classes such as string, fstream etc.
As my compiler was old(1992), it did not supported namespaces.

It did not matter to me until I intend to use the string class. For those who are not aware, string class is a built in class in the header file string(not string.h, I'll explain the difference shortly), that allows you to work with strings with more efficiency than the traditional c-type strings.


My compiler also did not had the standard library i.e. I was forced to use iostream.h instead of iostream and string.h instead of string.

Whats the difference between iostream and iostream.h?
Today's modern compilers(like G++,MS-Visual C++) don't allow/advice you use iostream.h or string.h instead they insist on iostream or string header file respectively. The immediate question the reader may ask is the difference between the iostream and iostream.h. I'll brief it out as this:
The iostream.h was the header file written by say, Bjarne Stroustrup, the creator of C++, and was distributed along the first c++ compiler. Later, different c++ compilers came up with different iostream.h and thus the language became compiler dependent.
Thus, to make C++ compiler independent, the need of standardisation was felt. Thus the C++ Standards Committee decided to cut the extension. This allowed the compiler companies to distribute their iostream.h along with the standard iostream.(for more detail about this topic, one can refer http://members.gamedev.net/sicrane/articles/iostream.html)

Now and thus, the said limitations forced me to find another compiler.

I then started using G++, and still use it. I recommend the use of G++ for some prominent reasons.

First of all, this the very standard compiler to date. Secondly, it is licensed under GPL and is free to use. It is cross-platform, i.e. you can make application under various operation systems like Windows, Linux etc. without(or with minimum) changing of your code.


My problem was thus sort out by shifting to a standard compiler and so do I advice everyone to use a standard compiler.

I am not against Turbo C++ 3.0 in the respect of its performance. But it is not a future compiler( forget future, it is not a present day compiler). Borland, the creator company of Turbo C++ does not make Turbo C++ anymore. They are with a new product called C++ Builder which is a decent/good compiler under $100. But my choice remains g++.
Why spend money on something when you can get a thing better than that for free.

Tuesday, August 7, 2007

Welcome to C++ Doubt Busters,
This Blog is the Spot where you can clear your doubts of C++.





This Blog will Cover the Important Question of C++ with there Solutions.