liveBook · Manning

Perror

As explained in Modern C

perror is a C standard library function that writes a diagnostic message describing the error indicated by errno.

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

How Modern C covers it

How deep it goes
Explains return-value checks, errno dependence, and resetting recovered error state, without detailing perror's implementation.
Examples
Uses failed puts and fopen operations to demonstrate diagnostic reporting before program termination.
What it assumes
Assumes basic C function calls and return values while teaching library-specific failure conventions and errno usage.
What you can do after
Diagnose failed C library operations with perror after checking their documented failure returns.
How it connects
Connects error handling, errno, return values, stdio, file streams, and process termination.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.