Problem With Using fgets()/gets()/scanf() After scanf() in C
Last Updated :
04 Jan, 2022
scanf() is a library function in C. It reads standard input from stdin. fgets() is a library function in C. It reads a line from the specified stream and stores it into the string pointed to by the string variable. It only terminates when either:
- end-of-file is reached
- n-1 characters are read
- the newline character is read
1) Consider a below simple program in C. The program reads an integer using scanf(), then reads a string using fgets(),
Input
10
test
C
// C program to demonstrate the problem when
// fgets()/gets() is used after scanf()
#include <stdio.h>
int main()
{
int x;
char str[100];
scanf("%d", &x);
fgets(str, 100, stdin);
printf("x = %d, str = %s", x, str);
return 0;
}
Output
x = 10, str =
Explanation: The problem with the above code is scanf() reads an integer and leaves a newline character in the buffer. So fgets() only reads newline and the string "test" is ignored by the program.
2) The similar problem occurs when scanf() is used in a loop.
Input:
a
b
q
C
// C program to demonstrate the problem when
// scanf() is used in a loop
#include <stdio.h>
int main()
{
char c;
printf("Press q to quit\n");
do {
printf("Enter a character\n");
scanf("%c", &c);
printf("%c\n", c);
} while (c != 'q');
return 0;
}
Output
Press q to quit
Enter a character
a
Enter a character
Enter a character
b
Enter a character
Enter a character
q
Explanation: We can notice that the above program prints an extra "Enter a character" followed by an extra newline. This happens because every scanf() leaves a newline character in a buffer that is read by the next scanf.
How to Solve the Above Problem?
- We can make scanf() to read a new line by using an extra \n, i.e., scanf("%d\n", &x) . In fact scanf("%d ", &x) also works (Note the extra space).
- We can add a getchar() after scanf() to read an extra newline.
The corrected programs for the above points will be,
1) scanf() when there is fgets() after it:
Input:
10
test
C
// C program to demonstrate the problem when
// fgets()/gets() is used after scanf()
#include <stdio.h>
int main()
{
int x;
char str[100];
scanf("%d\n", &x);
fgets(str, 100, stdin);
printf("x = %d, str = %s", x, str);
return 0;
}
Output
x = 10, str = test
2) When scanf() is used in a loop:
Input:
a
b
q
C
// C program to demonstrate the problem when
// scanf() is used in a loop
#include <stdio.h>
// Driver Code
int main()
{
char c;
printf("Press q to quit\n");
do {
printf("Enter a character\n");
scanf("%c\n", &c);
printf("%c\n", c);
} while (c != 'q');
return 0;
}
Output: Press q to quit
Enter a character
a
Enter a character
b
Enter a character
q
Must Read: Problem occurs with Scanner in Java when nextLine() is used after nextXXX()
Similar Reads
scanf("%[^
]s", str) Vs gets(str) in C with Examples gets()gets is a more convenient method of reading a string of text containing whitespaces.Unlike scanf(), it does not skip whitespaces.It is used to read the input until it encounters a new line.%[^\n]It is an edit conversion code.The edit conversion code %[^\n] can be used as an alternative to gets
2 min read
Taking String input with space in C (4 Different Methods) We can take string input in C using scanf("%s", str). But, it accepts string only until it finds the first space. There are 4 methods by which the C program accepts a string with space in the form of user input.Let us have a character array (string) named str[]. So, we have declared a variable as ch
2 min read
%n in scanf() in C with Example In C, %n is a special format specifier. In the case of printf() function the %n assign the number of characters printed by printf(). When we use the %n specifier in scanf() it will assign the number of characters read by the scanf() function until it occurs. Key points: It is an edit conversion code
2 min read
Why "&" is not used for strings in scanf() function? Below is syntax of Scanf. It requires two arguments: scanf("Format Specifier", Variable Address); Format Specifier: Type of value to expect while input Variable Address: &variable returns the variable's memory address. In case of a string (character array), the variable itself points to the firs
2 min read
Inbuilt library functions for user Input | sscanf, scanf_s, fscanf_s, sscanf_s The C Programming Language provides various Inbuilt Library Functions for User Input. In this article, we will learn about sscanf, scanf_s, fscanf_s, sscanf_s Library Functions in C. 1. sscanf() Function in C sscanf() is used to read formatted input from the string. Both scanf() and sscanf() functio
6 min read