Problem with scanf() when there is fgets()/gets()/scanf() After it

The scanf() function is a common C/C++ function. Even though the syntax is basic, it is vital to recognise several circumstances wherein its use would necessitate caution. One example is when fgets() is called after scanf ().

In this post, we'll look at why fgets() doesn't work after scanf(), as well as possible fixes. First, let's look at how the scanf() and fgets() functions perform.

In C, scanf() is a library function. It reads stdin for standard input. In C, fgets() is a library function. It reads a line from the supplied stream and saves it in the string variable linked to. It only ends when one of the following conditions is met:

  • End of the file is reached
  • n-1 characters are read.
  • The character from the newline is read.

Below is an illustration showing how we can utilise the scanf() function:

C Code:

Input:

Output:

Enter name: Box
Entered Name: Box

In C/C++, the fgets() function

fgets is an abbreviation for 'file get string' in the fgets() function. It is basically a function that reads up to n characters from a stream (file stream or standard input stream) and converts them to a string str. It is said as follows:

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

Let's go through every detail of this statement.

  • The amount of characters that must be read into the string is represented by int n.
  • FILE* stream is a reference to the file stream that contains the input is read. While accessing from the standard input, this is replaced by stdin.
  • This function's return value is the pointer to str.

Using fgets() after scanf() Issue

Assuming that we now understand how the fgets() and scanf() functions operate, let's examine what happens when we use the fgets() function immediately following the scanf() function and whether fgets() continues to function after scanf ().

Input:

Output:

7
a = 7, str = 
Box
dash: 2: Box: not found

The string "Box" is not printed, as is visible here. What is the cause of this? A specific function of the scanf() function is the cause of the issue.

In addition to reading data from the standard input stream, the scanf() function also inserts a newline character into the buffer. As a result, the scanf() function in the code above left a newline character after reading the integer a. After reading this newline character, the fgets() function ends the process (remember the three requirements we discussed for the fgets() action to finish reading input). As a result, the fgets() method disregards the string "Box" and does not print it.

It is crucial to keep in mind this aspect of scanf() anytime we employ it. But now the issue of fgets() failing to function after scanf() needs to be addressed. How can the string "Box" be printed as well?

Resolution for the Issue

Now that the issue of why fgets() fails after scanf() has been examined, a solution may be derived. Take note of the code below.

C Code:

Input:

Output:

a = 22, string = Box
  • Do anyone notice the distinction? In scanf("%dn", &a), an extra "n" has been added. This forces scanf() to read a new line. Additionally, we may do this using adding a space after the word "%d" as in scanf("%d ", &a).
  • after the scanf() method, add the getchar() function to read an additional newline.
  • As a result, the fgets() function will read the input string rather than the newline. Consequently, the issue of why fgets() fails after scanf is resolved ().

1. How can I get Fgets to perform after scanf?

Adding a "n" to scanf(), as in scanf("%dn", &x), or using getchar() after scanf() can both solve this problem.

2. What syntax is required to use scanf()?

The syntax for the scanf() function is as follows: int scanf(const char *format,...);

3. Why does fgets not perform after scanf?

Adding a "n" to scanf(), as in scanf("%dn", &x), or using getchar() after scanf() can both solve this problem. After reading this newline character, the fgets() function ended the operation.

4. What does Scanf in C return?

Scanf gives the total number of successful inputs or, in the case of an error, EoF (End of Line).

5. Is Fgets preferable to Scanf?

There are a few things to consider before making that choice. While scanf() only reads from standard input, fgets() can read from any file stream.

6. What syntax is required to use fgets()?

using fgets() looks like this: char* fgets(char* str, int n, FILE* stream);

Conclusion

If the scanf() function is used before the fgets() method, a very common issue can arise. Due to this problem, some of the input is not read by the fgets() function since the scanf() method leaves a newline character in the buffer. Adding a "n" to scanf(), as in scanf("%dn", &x), or using getchar() after scanf() can both solve this problem.


Next Topic#




Latest Courses