If fprinting to stderr fails, which error code should a C program return?
1
vote
1
answer
104
views
Assume the following C snippet showing a piece of error handling:
#include
#include
…
int main(int argc, char argv*[]) {
int retval=0;
…
if(3!=argc) { /* wrong number of arguments */
retval |= EX_USAGE;
const int fprintf_retval = fprintf(stderr, "Bad syntax.\n");
if (0>=fprintf_retval) retval |= … Hmm … ;
}
…
return retval;
}
Which error constant from sysexits.h should be placed instead of “… Hmm …”? We can say EX_OSERR (a failure to print an error message shows that something is off, though not necessarily deep in the operating system), or EX_IOERR (it's an output error, though not necessarily into a file), or EX_SOFTWARE (fairly general, but the reason for a failure to print an error typically lies outside the C program, and the error might relate to the operating system). Based only on the comments in sysexits.h, neither of the three constants fits perfectly. So what's the convention?
EDIT: In the scope of this question, we assume that the programmer does want to discern between certain kinds of errors via the exit code, and that more than one error can occur.
Asked by user743115
(1 rep)
Jul 24, 2025, 12:48 AM
Last activity: Jul 24, 2025, 09:15 AM
Last activity: Jul 24, 2025, 09:15 AM