ProgrammingHard

char b[5]; /* stdin holds the nine bytes: abcdefgh and a newline */ int calls = 0; do { fgets(b, sizeof b, stdin); calls++; } while (b[strlen(b) - 1] != '\n'); printf("%d", calls); How many times does the loop body run?

  1. A. 2
  2. B. 3
  3. C. 4
  4. D. 5

Correct answer: B. 3

fgets with size 5 reads at most 4 characters per call, so the input arrives as 'abcd', 'efgh' and finally a lone newline. The loop ends on the third call, when the last character is a newline.

More C Programming questions

Exams that ask this topic