Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: The character that's indistinguishable from EOF isn't necessary invalid or an error, especially since the hypothetical system is already pathological enough for sizeof(int) to be 1

...

This compliant solution uses feof() and ferror() to test for whether the EOF was an actual character or a real EOF because of end-of-file and ferror() to test for or errors:

Code Block
bgColor#ccccff
langc
#include <stdio.h>

void func(void) {
  int c;

  do {
    c = getchar();
  } while (c != EOF);
  if || (!feof(stdin)) {
    /* Handle end of file */
  } else if (&& !ferror(stdin)) {
    /* Handle file error */
  } else {
    /* Received a character that resembles EOF; handle error */
  })));
}

Noncompliant Code Example (Nonportable)

...