fgets(3) Library Functions Manual fgets(3)

fgets - FILE get line-string

Standard C library (libc-lc)

#include <stdio.h>
char *fgets(int size;
            char s[restrict size], int size, FILE *restrict stream);

fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ('\0') is stored after the last character in the buffer.

fgets() returns s on success.

When end of file occurs while no characters have been read, NULL is returned.

On error, NULL is returned, and errno is set to indicate the error.

See fgetc(3).

For an explanation of the terms used in this section, see attributes(7).

Interface Attribute Value
fgets () Thread safety MT-Safe

C11, POSIX.1-2008.

POSIX.1-2001, C89.

ferror(3), fgetc(3), fgetws(3), fopen(3), fread(3), getline(3), unlocked_stdio(3)

2026-08-19 Linux man-pages 6.19