1

I came into a situation where I have to take the exit status of my program that is the number returned by exit() function.

in LINUX, using $? command i got the status. Is there any other method to get this status which can work in windows too ?

one other thing I noticed is that if we pass a negative number to exit() function it crashes my IDE and $? command not able to give status in this case too. I checked exit() function its reference says we can pass a integer.

SingerOfTheFall
  • 29,228
  • 8
  • 68
  • 105
birubisht
  • 890
  • 8
  • 16

1 Answers1

3

Program return values mean different things on different operating systems. On Unix-y systems, only values 0-255 are valid (1 unsigned byte). Note this means negative values are not allowed, and may be the cause of your IDE problems. FreeBSD introduced a load of specific meanings in sysexits.h, and while this certainly exists on Linux distributions I've used, the only real universally accepted one is 0 for success and non-0 for failure. As Pete Becker points out, EXIT_SUCCESS and EXIT_FAILURE are standard defined macros (0 and 1 respectively on Unix-y platforms, but implementation defined).

On windows, you get a 32 bit signed integer (negatives are allowed, don't know if they mean anything). Again, 0 is success, and other values mean various things. Like echo $?, you can use echo %errorlevel% to see the last program's exit code in the console.

Also note that my N3337 (draft very close to C++11 standard) says at section 3.6.1 paragraph 5:

If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;

BoBTFish
  • 19,167
  • 3
  • 49
  • 76