What does return 0 mean? /does std::endl also do std::flush?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

What does return 0 mean? /does std::endl also do std::flush?

Post by MasterGod »

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.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
marcos
Posts: 16
Joined: Fri May 16, 2008 5:38 pm

Post by marcos »

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 :P

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.
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Cool thanks.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
greenya
Posts: 1012
Joined: Sun Jan 21, 2007 1:46 pm
Location: Ukraine
Contact:

Post by greenya »

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:

Code: Select all

void exit(int exit_code);
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:

Code: Select all

The program '[324] MyApp1.exe: Native' has exited with code 0 (0x0).
Legend:
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();
}
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.
Post Reply