You ask for a number, then a letter. The program takes the number, prints the second question and ends before you can type anything. When scanf skips input like this, nothing is wrong with your keyboard. A leftover Enter key is the cause, and the fix is one space.
The Problem: scanf Skips Input After a Number
Here is a small program that asks for an age and then a grade:
#include <stdio.h>
int main(void) {
int age;
char grade;
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your grade (A-F): ");
scanf("%c", &grade);
printf("Age %d, grade %c\n", age, grade);
return 0;
}
Type 15 and press Enter. The program prints the grade question and finishes straight away. The output is Age 15, grade followed by an empty line where the grade should be. You never got to type it.
Why It Happens
Everything you type goes into the input buffer first, and that includes the Enter key. After you type 15 and press Enter, the buffer holds 15 and a newline character.
%d reads the 15 and stops. The newline stays in the buffer. Then %c runs, and %c reads exactly one character, whatever it is. The newline is a character. So it takes that and never waits for you.
This is also why two numbers in a row work fine. %d, %f and %s skip spaces and newlines before they start reading. %c doesn’t.
The Fix: One Space Before %c
Put a space in front of the format:
scanf(" %c", &grade);
A space in a scanf format means “skip any spaces, tabs and newlines here”. So the leftover Enter is thrown away, and %c reads the first real character you type. Run it again with 15 and then B, and you get Age 15, grade B.
When scanf Skips Input Before fgets
The same leftover newline breaks fgets. If you read a number with scanf and then a name with fgets, the name comes back empty, because fgets stops at the first newline it sees. A space can’t help here. Clear the rest of the line yourself:
#include <stdio.h>
int main(void) {
int age;
char name[50];
printf("Enter your age: ");
scanf("%d", &age);
int c;
while ((c = getchar()) != '\n' && c != EOF) {
/* throw away the rest of the line */
}
printf("Enter your name: ");
fgets(name, sizeof name, stdin);
printf("Age %d, name %s", age, name);
return 0;
}
The small while loop reads one character at a time until it reaches the newline. After that the buffer is empty and fgets waits for the name.
Don’t use fflush(stdin) for this. It seems to work with some compilers, but the C standard doesn’t define what it does for input, so it can fail on another machine.
Quick Rule
%cafter any other input: write%cwith a space in front.fgetsafterscanf: clear the line first with thegetcharloop.Lots of mixed input: read every line with
fgetsand convert it withsscanforstrtol. Then nothing is ever left behind.
Every case where scanf skips input comes back to that one leftover character. The scanf reference on cppreference lists which formats skip whitespace and which don’t.
A related bug is the program that loops forever when you type a letter. I covered it in why your C program loops forever. For the basics, read my complete guide to user input in C.
Did your program skip a question too? Paste the scanf lines in the comments, and I’ll tell you where the space goes.
Originally published on my blog. I post new C, Python and AI-agent guides there regularly.

Top comments (0)