I see in a lot of legacy software and bad tutorials on the Internet that recommend using exit(-1)
, return -1
or similar to represent "abnormal termination". The problem is, in POSIX at least, -1
has never been and is not a valid status code. man 3 exit
illustrates that exit()
returns the value of status & 0377
to the parent, meaning that -1
becomes 255
. On non-POSIX systems, EXIT_FAILURE
is recommended for portability. But I never see "-1 means abnormal termination" in conjunction with "EXIT_FAILURE may be something other than 1", indicating that they clearly believe "-1" is conventional even on non-POSIX systems.
Here's an example of a StackOverflow question that perpetuates this. The software "unrealircd" is also an example of a program that uses exit(-1)
to terminate the program. In practice, this makes it difficult to interface with systemd
.
Where did this anti-pattern come from? Is it valid in some context?
unsigned char
.char
since its range of values is -128 to 127. Further, I already stated "-1" gets converted to "255" in my question body.