Hi all..i wud b grateful if u cud explain to me when to use int main and void main, wat is the difference and when to use return 0 or 1 in c++..thx
int main and void main
Collapse
X
-
In C++, you can NEVER use a void main(). It must always be int main().
in C++, returning a 1 inside main indicates that there was an error. A common occurrence of this is when you are trying to input from a file, but the file cannot be loaded. It correctly exits the program, but with the added note that there was some error. Returning 0 indicates that everything worked just as planned. -
actually you can use void main in C++ it just does not return anything i used it all the time in Turbo C++ if you do use it make sure to put a getch() at the end so the program pauses for the user to press a keyComment
-
In the C world, you can use either the void or int format. Some C compilers issue a warning about the void main, but still compile the program.
The C++ world took a hard line approach to main. Now you get an error if you don't use int, end of story. I was surprised when I first saw it. I don't know why they took this hard line approach. Good intentions to force good coding? Who knows.
There is a 3rd approach: Don't define a return value for main, at all. The gnu c++ compiler spits out a warning but will compile it.Comment
-
no you can use void main in some compilers i don't know about the new TC but in the old dos Turbo C++ you can use void mainComment
Comment