
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
Problem with scanf() when there is fgets()/gets()/scanf() after it in C
The problem states that what will be the working or the output if the scanf is followed by fgets()/gets()/scanf().
fgets()/gets() followed by scanf()
Example
#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
Input: 30 String Output: x = 30, str =
Explanation
fgets() and gets() are used to take string input from the user at the run time. I above code when we run enter the integer value then it won’t take the string value because when we gave newline after the integer value, then fgets() or gets() will take newline as an input instead of the desired input which is “String”.
scanf() followed by a scanf()
To repeatedly get scanf() followed by a scanf() we can use a loop.
Example
#include<stdio.h> int main() { int a; printf("enter q to quit"); do { printf("
enter a character and q to exit"); scanf("%c", &a); printf("%c
", a); }while (a != ‘q’); return 0; }
Output
Input: abq Output: enter q to quit enter a character and q to exita a enter a character and q to exit enter a character and q to exitb b enter a character and q to exit enter a character and q to exitq
Explanation
Here we can see an extra line ‘enter a character and q to exit’ after an extra new line, this is because each time scanf() leaves a newline character in a buffer which is being read by he next scanf() followed by it. To solve this problem with using ‘
’ with the type specifier in scanf() like scanf(“%c
”); or the other option is we can use extra getchar() or scanf() to read extra new line.
- Related Articles
- Difference between scanf() and gets() in C
- scanf() and fscanf() in C
- Return values of printf() and scanf() in C
- fgets() and gets() in C
- How to simulate scanf() method using Python?
- What is the common error occurred while using scanf() statement in C language?
- Where is the MySQL database gets saved when it is created?
- Explain why, when a burning candle is covered with an inverted gas jar, the candle gets extinguished after sometime.
- When Ag is exposed to air it gets a black coating of
- fgets() function in PHP
- When is a semicolon after } mandated in C++ Program?
- Why there is a slight loss in weight in gold jewellery when it is given for polishing?
- fgets() and fread() - What is the difference in PHP?
- Why is the closing of a dandelion flower at dusk (when it gets dark), not a tropism?
- What does it mean when a numeric constant in C/C++ is prefixed with a 0?
