Calling perror will give you the interpreted value of errno , which is the local local error value recorded using POSIX system calls (i.e., each thread has its own value for errno ). For example, if you made an open() call, and a generated error occurred (i.e., it returned -1 ), you can immediately call perror to find out what the actual error is. Keep in mind that if you make other system calls during this time, then the value will be written to errno more, and the perror call perror not be useful in diagnosing your problem if the error was generated by an earlier syscall.
fprintf(stderr, ...) on the other hand, can be used to print your own error messages. When printing to stderr you avoid displaying error messages mixed with the "normal" output, which should go to stdout .
Keep in mind that fprintf(stderr, "%s\n", strerror(errno)) is similar to perror(NULL) , since calling strerror(errno) generates a printed string value for errno , and you can combine it with any other custom error message via fprintf .
Jason Aug 24 2018-12-12T00: 00Z
source share