ProgrammingModerate

Which call reads a line into char buf[100] safely?

  1. A. gets(buf)
  2. B. scanf("%s", buf)
  3. C. fgets(buf, sizeof buf, stdin)
  4. D. fgets(buf, 1000, stdin)

Correct answer: C. fgets(buf, sizeof buf, stdin)

fgets is given the real capacity, so it never writes more than 100 bytes. gets is unbounded and removed from C11, scanf %s stops only at whitespace, and the 1000 limit overflows the 100-byte buffer.

More C Programming questions

Exams that ask this topic