5

Why do we use 42 as an argument of exit while exiting the process? I am wondering is it some macro value (like 1 is value of EXIT_FAILURE macro) or it has some deeper meaning?

if(pid == 0) {
    printf("something\n");
    exit(42);
}

It is kind of clear that it doesn't matter if I use exit(1) or exit(42), but why just 42?

zeroDivider
  • 1,050
  • 13
  • 29
  • 13
    42 is [the answer to life, the universe and everything!](https://www.google.it/?client=ubuntu#channel=fs&q=the+answer+to+life%2C+the+universe+and+everything&gfe_rd=cr) – Bakuriu Sep 07 '16 at 09:41
  • 2
    Because 127 was taken? – EOF Sep 07 '16 at 09:42
  • 6
    42 is clearly the *opposite* of a named value -- it's a magic value that only the author of the code knows why it's there. It's not portable, though, the only portable exit values are 0, `EXIT_SUCCESS` and `EXIT_FAILURE`. – Kerrek SB Sep 07 '16 at 09:42
  • 3
    To be more explicit about what others have said: Don't think too hard about it. It's just a pop culture reference without any special significance to the C language. – Cubic Sep 07 '16 at 09:43

3 Answers3

11

Any number except for 0 would have done. But 42 is the Answer to the Ultimate Question of Life, the Universe, and Everything.

Very popular among IT people...

But why did Douglas Adams pick 42?

I sat at my desk, stared into the garden and thought '42 will do'. I typed it out. End of story

Codo
  • 75,595
  • 17
  • 168
  • 206
2

Such magic value may be used to indicate exact exit reason to parent process. You may threat it like a some kind of minimalistic IPC. Of course both processes must agree about actual values and their meanings, as well as do not use special reserved exit codes.

Sergio
  • 8,099
  • 2
  • 26
  • 52
  • 1
    If possible, values from [sysexits.h](https://www.freebsd.org/cgi/man.cgi?query=sysexits&apropos=0&sektion=0&manpath=FreeBSD+4.3-RELEASE&format=html) should be preferred. It's fairly standard. – λuser Sep 07 '16 at 09:49
2

It is kind of clear that it doesn't matter if I use exit(1) or exit(42)

It actually matters a lot.

The exit code can be used by the process that launches the exiting process to know how it completed and why it failed.

The process that launches your program can inspect the value of the environment variable $? immediately after your program completes to know if it succeeded or why it failed, if it didn't succeed.

Let's say your program downloads a file from a remote site and stores it in a local directory. It expects to use an existing local directory and it doesn't attempt to create it if it doesn't exist. It can exit, for example, with code 37 when the remote file cannot be downloaded because the remote site return 404 Not Found, with code 62 when it cannot download the file because the network is down (or a timeout happens) and code 41 when the local directory does not exist.

A bash script, for example, that invokes your program can check the value of the environment variable $? immediately after your program completes. If its value is 37 (remote file is not found) it must not attempt to retry because the error is permanent. On exit code 62 (network issues) it can wait a couple of seconds and try again (the error condition is transient, it could disappear after a while). On exit code 41 (local directory not found) it can create the local directory then launch your program again (a precondition was not met).

axiac
  • 68,258
  • 9
  • 99
  • 134
  • 1
    Are 37, 62 and 41 just random numbers you just typed, or is it a certain convention? – SOFe Feb 13 '19 at 04:15
  • Random numbers between `1` and `127`. `0` is the exit code for success, values larger than `127` are not interpreted the same way on all Unix systems. – axiac Feb 13 '19 at 05:27
  • I see. The way you are referencing 37/62/41 in the answer looks as if it is the standard of a certain class of programs. (Just like `--gnu-options` is a standard in GNU programs but not in Windows or Java programs etc.) – SOFe Feb 13 '19 at 05:32