Darrell Grainger said:
For example, strtol will return LONG_MAX or LONG_MIN if a string is out of
range and set errno to ERANGE. So if strtol fails due to out of range, I
would use perror.
I have found that it is useful, in practice, to include the value of
errno in the output. These days, the error message output by perror
is often translated. This is good for the user, as they get an error
in a language they understand, but bad for me, if it is in a language
I do not understand. Therefore, I tend not to use perror and instead
use something like this:
fprintf(stderr, "Error: Can't open file: %d: %s\n",
errno, strerror(errno));
If there is an error, I can look up the reason using the numerical
error code regardless of what the text looks like.
(If I wanted to be really friendly towards the user, I'd use gettext
or a similar package to translate the format string into their preferred
language. I'll elide that in the interest of brevity.)