Two questions that bother me lately:
1. What does it mean when we return 0 in main()? Why not returning 1 or -1 or anything else..? Is it just a habit people tend to follow?
2. I've recently learned what is std::flush and I've heard that maybe std::endl also flushes the buffer. Is it true?
Thanks!!!
P.S
Google gave some answers but I trust You guys more on this one.
What does return 0 mean? /does std::endl also do std::flush?
1. Main returning "0" means that the program executed successfully. Having "void main" is a bad practice and most modern compilers raise a Warning. Why? Returning a error code in the main body is one of the quickest and cleanest ways to point out what went wrong (have a look at windows getLastError and linux errno.h) for example when using a batch script to call an exec, when using winexec in win C++, Runtime.exec in java...
In most languages/compilers 0 means false. So you could see this return value as the answer to the question: Did something go wrong? BUT do not assume that false is always 0. As i said "MOST" compilers
What would you do instead? parsing the standard or error output.
2. Yes. cout and cin are buffered outputs. To improve the portability of the code you should always call flush after finishing writing. But if you use "endl", you don't need to flush because "endl" also calls flush.
In most languages/compilers 0 means false. So you could see this return value as the answer to the question: Did something go wrong? BUT do not assume that false is always 0. As i said "MOST" compilers
What would you do instead? parsing the standard or error output.
2. Yes. cout and cin are buffered outputs. To improve the portability of the code you should always call flush after finishing writing. But if you use "endl", you don't need to flush because "endl" also calls flush.
main() must return a value to say OS what was wrong. 0 means no error. any other values may indicate error but the OS is doing nothing in any case, but just gives to a programmer an ability to check the value.
If you use "void main()" and of cause "return;" -- your app always exits with Exit Code 0. Anyway, you always has another cool function:
It will exit app with desired Exit Code in any case.
For example, if you using Visual Studio, you can run app and close it. Open Output window, last line will be something like:
Legend:
324 == Process Id
MyApp1.exe == Executable module name
0 (0x0) == Exit Code (the code which you specified in main()' return operator)
Usage sample:
Exit Code very useful if you are writing some command line utility. Indeed good-written command line utility must always carry about Exit Code (not only show error message to a user), since it can be used by another application in background -- so it will know what is wrong easily.
That should be clear.
If you use "void main()" and of cause "return;" -- your app always exits with Exit Code 0. Anyway, you always has another cool function:
Code: Select all
void exit(int exit_code);
For example, if you using Visual Studio, you can run app and close it. Open Output window, last line will be something like:
Code: Select all
The program '[324] MyApp1.exe: Native' has exited with code 0 (0x0).
324 == Process Id
MyApp1.exe == Executable module name
0 (0x0) == Exit Code (the code which you specified in main()' return operator)
Usage sample:
Code: Select all
int main()
{
if (!checkCommandLine())
{
printf("Command line has errors.");
return 1;
}
if (!checkRAMAvailability())
{
printf("Not enough RAM.");
return 2;
}
return allOKSoDoSomething();
}
That should be clear.