C Language: fgets function(Read String from File)

In the C Programming Language, the fgets function reads characters from the stream pointed to by stream.

The fgets function will stop reading when n-1 characters are read, the first new-line character is encountered in s, or at the end-of-file, whichever comes first. Then the fgets function will append a null character to the string.


The syntax for the fgets function in the C Language is:

char *fgets(char *s, int n, FILE *stream);

Parameters or Arguments


The array where the characters that are read will be stored.


The size of s.


The stream to read.


The fgets function returns s. The fgets function will return a null pointer if an error occurs while trying to read the stream or the end of the stream is encountered before any characters are stored.

Required Header

In the C Language, the required header for the fgets function is:

#include <stdio.h>

Applies To

In the C Language, the fgets function can be used in the following versions:

  • ANSI/ISO 9899-1990

stdio.h Functions