The fgets() function in C/C++
The fgets in the fgets() function stands for ‘file get string’. This is essentially a function that is used to read upto n characters from the stream (file stream or standard input stream) into a string str. It is declared as:
char* fgets(char* str, int n, FILE* stream);
Let’s discuss every aspect of this declaration.
- int n refers to the number of characters that need to be read into the string.
- FILE* stream is the pointer to the file stream from which the input is being read. This is replaced by stdin when reading from the standard input.
- The pointer to str is the return value of this function.
Let’s take an example to understand how fgets() reads from a file.
# include <stdio.h>
int main( )
{
FILE *file ;
char str1[40] ;
printf( "The file sample.txt is opened in read mode" ) ;
file = fopen( "sample.txt", "r" ) ;
if ( file == NULL )
{
printf( "sample.txt couldn't be opened" ) ;
return 0;
}
printf( "sample.txt is being read." ) ;
while( fgets ( str1, 40, file) != NULL )
printf( "%s" , str1 ) ;
printf("The file sample.txt is now closed") ;
fclose(file) ;
return 0;
}

You can also try this code with Online C++ Compiler
Run Code
Output:
Opening the file test.c in read mode
Reading the file test.c
Welcome to Coding Ninjas
Closing the file test.c
The fgets() function terminates its operation if one of the following conditions is fulfilled. These conditions will be important to keep in mind during our discussion on why fgets() doesn’t work after scanf().
- A newline character is encountered
- The function has read n - 1 characters
- EOF (End of File) has been reached.
You can also read about C dynamic array, Tribonacci Series
Problem with using fgets() after scanf()
Now that we know how the fgets() and scanf() functions work, let’s look at what happens if we use the fgets() function right after the scanf()function and whether fgets() doesn’t work after scanf().
int main(){
int x;
char str[50];
scanf(“%d”, &x);
fgets(str, 50, stdin);
printf(“x = %d, str = %s”, x, str);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Input:
5
Ninja
Output:
x = 5, str =
We can see here that the string “Ninja” is not printed. What is the reason behind this? The problem is because of a certain feature of the scanf() function.
When scanf() reads input from the standard input stream, it also creates a newline character in the buffer. So in the above code, after reading the integer x, the scanf() function left a newline character. The fgets() function then reads this newline character and terminates the operation (recall the three conditions that we discussed for the fgets() operation to stop reading input). Therefore the string “Ninjas” is ignored by the fgets() function and does not get printed.
This feature of scanf() is something that is important to note whenever we use this function. But now the question is, is there a solution to this problem of how fgets() doesn’t work after scanf()? How do you print the string “Ninjas” as well?
Solution to the problem
After assessing the problem of why fgets() doesn’t work after scanf(), it is time to derive a solution. Observe the code below.
int main(){
int x;
char str[50];
scanf("%d\n", &x);
fgets(str, 50, stdin);
printf("x = %d, str = %s", x, str);
return 0;
}

You can also try this code with Online C Compiler
Run Code
Input:
10
Ninjas
Output:
x = 10, str = Ninjas
Do you see the difference? We have introduced an extra “\n” in scanf(“%d\n”, &x). By doing this, we make scanf() read a new line. We can also achieve this by
- Introducing an extra space as in scanf(“%d “, &x).
- Adding a getchar() function after the scanf() function to read an extra newline.
Therefore, the fgets() will not have to read the newline and will read the string input. Thus solving the problem of how fgets() doesn’t work after scanf().
Frequently Asked Questions
How do I make Fgets work after scanf?
This can be solved by introducing a “\n” in scanf() as in scanf("%d\n", &x) or by adding getchar() after scanf().
Why fgets doesn't work after scanf?
This can be solved by introducing a “\n” in scanf() as in scanf("%d\n", &x) or by adding getchar() after scanf(). The fgets() function then read this newline character and terminated operation
Is Fgets better than Scanf?
There are certain aspects to look at before deciding so. fgets() can read from any file stream but scanf() only reads from standard input.
What is the syntax for using scanf()?
Here is the syntax for using scanf(),
int scanf( const char *format, ... );
What is the syntax for using fgets()?
Here is the syntax for using fgets(),
char* fgets(char* str, int n, FILE* stream);
What does Scanf return in C?
Scanf returns the total number of inputs successful or EoF(End of Line) if there is an error.
Conclusion
While using the scanf() function, a very common problem is faced if it is used before an fgets() function. Because of this issue, the fgets() function does not read some part of the input as the scanf() function leaves a newline character in the buffer. This can be solved by introducing a “\n” in scanf() as in scanf("%d\n", &x) or by adding getchar() after scanf().