1

I need to decide which exit code to exit() with in different scenarios. I just read

Are there any standard exit status codes in Linux?

but I need to write something that is (sort of) cross-platform Linux+Windows. MS Windows does not seem to have something like /usr/include/sysexits.h; it only has C89's stdlib.h, which provides

#define EXIT_SUCCESS    0
#define EXIT_FAILURE    1

So are these two the only thing that's portable? Or does Windows have some more elaborate platform-standard exit codes?

Note: I don't mean the System Error Codes of course.

Community
  • 1
  • 1
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • The de-facto standard is to either return 0/1 or to return a system error code (or exception code). But not everybody follows it. – Harry Johnston Jul 21 '15 at 00:24
  • @HarryJohnston: Reference to this being the standard? – einpoklum Jul 21 '15 at 04:08
  • Actually the most common behaviour is probably to just return 0. Most GUI programs assume nobody will be looking at the return code anyway, which I suppose is true most of the time. Personally I prefer to return an error code, or 1 if there is no specific error code applicable. – Harry Johnston Jul 21 '15 at 04:32

2 Answers2

1

Is there a Windows equivalent of standard(ish) UN*X process exit codes?

No, as in practise there are not IXis standard exit codes (besides that 0 indicates success and everything else something else).

alk
  • 69,737
  • 10
  • 105
  • 255
1

There are a few exit codes that you often see on Windows if a process doesn’t terminate normally. These are large negative numbers like -1073741510, and are defined in ntstatus.h. The most common ones are:

0xC0000005STATUS_ACCESS_VIOLATION: basically the equivalent of a segfault
0xC0000409STATUS_STACK_BUFFER_OVERRUN
0xC000013ASTATUS_CONTROL_C_EXIT: application was interrupted with Ctrl+C

There is a large table on MSDN although I don’t think you will see most of them as exit codes.

For application defined codes, the story is the same as on Linux. Usually you pick some exit codes and describe them in the documentation.

For those writing batch files, be aware that CMD interprets the exit codes above as negative signed integers, so the test IF ERRORLEVEL 0 will succeed with these codes. So use the more strict equality test IF %ERRORLEVEL% NEQ 0.

roeland
  • 5,349
  • 2
  • 14
  • 28