Is checking the return value of printf important?

For clarity - printf() returns ...

The printf function returns the number of characters transmitted, or a negative value if an output or encoding error occurred. C11 §7.21.6.3 3


Checking the return value of printf() for a negative value is pedantic, and is not usually needed. One could consider the following cases:


Environmental limits.

A single printf() with "%s" may exceed an environmental limit and cause printf() to return a negative value. This would not imply the a subsequent message on fprintf(stderr, ... must also fail.

The number of characters that can be produced by any single conversion shall be at least 4095. C11 §7.21.6.1 15


Weak output devices.

A case where stdout is known to be often re-directed over a communication interface where output failures need to be detected. Even though a screen output has extraordinary high success, this is not so with various others output streams like serial (rs232). In this case stdout and stderr may re-direct differently and so stderr may remain reliable.


In any case, if the professor grades on a curve, likely many incurred the same minus point - so no grade difference. Get use to customers with odd requirements and expectations.


The Unix philosophy is that stdout (though not necessarily stderr) should be further processable. Compilers and generators use it for code output. stdout should be where your process's product goes. If that product is cut short, your processes shouldn't be returning EXIT_SUCCESS. I say do check those writes to stdout.

(stderr, on the other hand, is more or less for convenience. If you're using it, you're probably in a state of error already anyway, and if your error reporting fails, there's not much you can do (though you should still signal the error with return codes).)


Not checking a return value is considered bad practice. BUT it is considered clean, if you explicitly state that you ignore the return value by adding (void) in front of the function call:

(void) printf(...);

This indicates, that you know there is return value, but you are intentionally ignoring it.


Generally speaking, you should always check the return value of a function for errors.

In the case of printf however, there is little use in doing so in most cases. As you mentioned, if it does fail you could use fprintf to print to stderr, but then that raises the question of should that be checked for error.

If you don't redirect or reopen stderr you'll likely have the same issue, in which case it probably doesn't matter, but if stderr is pointing someplace else then writing there could have value. You could also exit the process but you need to determine if it makes sense to do so.

One notable time you might want to check the return value is if you want to keep track of how many characters you printed for formatting purposes. I've done this with fprintf when writing to a log file to determine when to roll the log, but since printf generally writes to an interactive console (and if it's not due to redirection, you wouldn't know it), that wouldn't really apply.

As for your professor, my only guess is that he wants you to get into the habit of checking for errors. That is a Good Thing, however like most rules there are exceptions, and this is one of them.