When should I use perror("...") and fprintf(stderr, "...")?

CStderr

C Problem Overview


Reading the man pages and some code did not really help me in understanding the difference between - or better, when I should use - perror("...") or fprintf(stderr, "...").

C Solutions


Solution 1 - C

Calling perror will give you the interpreted value of errno, which is a thread-local error value written to by POSIX syscalls (i.e., every thread has it's own value for errno). For instance, if you made a call to open(), and there was an error generated (i.e., it returned -1), you could then call perror immediately afterwards to see what the actual error was. Keep in mind that if you call other syscalls in the meantime, then the value in errno will be written over, and calling perror won't be of any use in diagnosing your issue if an error was generated by an earlier syscall.

fprintf(stderr, ...) on the other-hand can be used to print your own custom error messages. By printing to stderr, you avoid your error reporting output being mixed with "normal" output that should be going to stdout.

Keep in mind that fprintf(stderr, "%s\n", strerror(errno)) is similar to perror(NULL) since a call to strerror(errno) will generate the printed string value for errno, and you can then combined that with any other custom error message via fprintf.

Solution 2 - C

They do rather different things.

You use perror() to print a message to stderr that corresponds to errno. You use fprintf() to print anything to stderr, or any other stream. perror() is a very specialized printing function:

perror(str);

is equivalent to

if (str)
    fprintf(stderr, "%s: %s\n", str, strerror(errno));
else
    fprintf(stderr, "%s\n", strerror(errno));

Solution 3 - C

perror(const char *s): prints the string you give it followed by a string that describes the current value of errno.

stderr: it's an output stream used to pipe your own error messages to (defaults to the terminal).

Relevant:

char *strerror(int errnum): give it an error number, and it'll return the associated error string.

Solution 4 - C

perror() always writes to stderr; strerr(), used together with fprintf(), can write to any output - including stderr but not exclusively.

fprintf(stdout, "Error: %s", strerror(errno));
fprintf(stderr, "Error: %s", strerror(errno)); // which is equivalent to perror("Error")

Furthermore, perror imposes its own text formating "text: error description"

Solution 5 - C

Perror function take more time to perform execution call goes from user space to kernal space wheras fprintf calls goest to api to kernal

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionfreeboy1015View Question on Stackoverflow
Solution 1 - CJasonView Answer on Stackoverflow
Solution 2 - Cfreeboy1015View Answer on Stackoverflow
Solution 3 - CAdib SaadView Answer on Stackoverflow
Solution 4 - CSebastienView Answer on Stackoverflow
Solution 5 - Cvivek singhView Answer on Stackoverflow