Practice mode C Language

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.