How does fgets(buf, n, fp) behave when it reads a line?
Correct answer D. It reads at most n - 1 characters, keeps the newline if read, and adds a null terminator
Explanation
fgets stops after n - 1 characters, at a newline or at end of file, and always writes a terminating null, so it cannot overflow buf. Unlike the removed gets function, it keeps the newline in the buffer, which programs often strip with buf[strcspn(buf, "\n")] = '\0'. It returns NULL on end of file or error.
Answer checked How we write and check questions
Discussion