简体   繁体   中英

Is checking the return value of printf important?

In one of my University Projects, I got points off and feedback from my professor saying I didn't handle printf errors.

In English --> / * ### FB: Error handling printf () is missing * /

/* ### FB: Fehlerbehandlung printf() fehlt */
    printf("%7lu %8lld %10s %3lu %-8s %-8s %8lu %12s  %s %s %s\n",
           sb->st_ino, nblks, permstr, (unsigned long) sb->st_nlink,
           username, groupname, sb->st_size,
           ntime, filename, (symlink ? "->" : ""),
           (symlink ? symlink : "")
           );

My question is, is it really important to always check return value of the printf function and handle the errors? Even if I find an error I will still use fprintf to print to stderr , for which I have to check the return type for fprintf again.

So when should the return value be checked, and how should it be handled?

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.

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.

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.

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).)

No. In the real world, as opposed to an academic exercise in a culture mit einem Ruf für Pedanterie , it's not important to check the return value of printf() , or close() , or quite a few other things. If there's no reasonable way to act on a piece of information, why bother collecting it?

If you are writing an app that spits JSON (for example: read from DB export the data into stdout ) the caller of your program might redirect the output into a file. In such case - the disk might get filled.

Question: who will complain about the disk being full: the shell or your program?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM