what is the difference between exit(0) and exit(1)?
is it same??
what is the difference between exit(0) and exit(1)?
is it same??
Both halt the program, but return different values to the OS.
Generally, when a program ends, it returns an exit value to the OS. Traditionally an exit value of zero tells the OS that the program ran to completion without problems. Nonzero values tell it that something went wrong and the program halted early, with each different non-zero value corresponding to a different thing that went wrong in the program. For example 1 might mean that you didn't give it enough command line arguments, 2 might mean that it couldn't allocate enough memory, etc.
Code:while(!asleep) { sheep++; }
what TheBigH said. one effect of returning the 0 or nonzero error codes is that if you don't do it right (or for example declare 'void main(void)', your program won't work right in 'make' and scripts that check return codes. even if you aren't using make or scripts now, its good practice to do it right.
I don't know what you mean by "make scripts"... but you are on the right track.
I'm more familiar with windows, so I'll give an examle from there...
In batch files the returned value from a program is made available in the ERRORLEVEL value which can be tested in the IF statement, allowing batch files to branch according to the returned values from programs.
Similarly when a "child" program is launched with CreateProcess() the returned value from the program can be inspected and is made available in the GetExitCodeProcess() function. This can be used to test the result of running the program.
So void main() just doesn't cut it now... in fact it didn't cut it then either. Even back in the good old DOS days I sometimes had batch files go silly on me because a program wasn't returning what it should.
The thing to keep in mind is that with void main() your program may run just fine but it will not be correctly interfaced to the operating system. The exit(errorlevel) and return errorlevel; functions do have meaning to the OS. If you don't use them the OS gets garbage and programs that rely on those values may behave erratically.
Last edited by CommonTater; 12-07-2011 at 02:46 PM.
thank you...
If in doubt, if you have a define of EXIT_SUCCESS or EXIT_FAILURE available on your system, return those. GCC supports them, for instance:
If you run your code via something like Eclipse or another IDE, generally the program will be considered to have "failed" if it didn't return 0 (EXIT_SUCCESS). Not usually important, but for future reference, it's good to know why.Code:if(it_worked) { return(EXIT_SUCCESS); } else { return(EXIT_FAILURE); };
Those are actually standard macros available via <stdlib.h>Originally Posted by ledow
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)