Report a failed output operation
if (puts("hello world") == EOF) { perror("can't output to terminal:"); exit(EXIT_FAILURE); } Check puts before calling perror. The EOF return signals failure; perror adds a diagnostic based on errno, then exit stops execution. section 8.1.3
perror is a C standard library function declared by stdio.h. It reports the error state associated with a failed library operation, optionally prefixed by the supplied message. The operation itself must still be checked first: library functions use different failure conventions, and perror does not replace that check. section 8.1.3
Use perror as part of a failure-handling sequence:
- Check the function's documented failure return value. section 8.1.3
- Call perror while errno describes the relevant failure. section 8.1.3
- Recover only when appropriate; otherwise terminate or propagate the failure. section 8.1.3
