Basics Lesson 6 of 16
C User Input with scanf and fgets
Read numbers, characters and words with scanf and the & operator, read full lines with fgets, check scanf's return value, and avoid common input bugs.
To make programs interactive, you need to read what the user types. C's standard tool is scanf, the input partner of printf. It reads values using the same kind of format specifiers.
Reading numbers
#include <stdio.h>
int main(void) {
int age;
double height;
printf("Enter your age: ");
scanf("%d", &age);
printf("Enter your height in meters: ");
scanf("%lf", &height);
printf("In 5 years you'll be %d. Height: %.2f m\n", age + 5, height);
return 0;
}
Output
Enter your age: 16 Enter your height in meters: 1.62 In 5 years you'll be 21. Height: 1.62 m
%dreads anint.%lf(long float) reads adouble. Note:printfuses%ffor doubles, butscanfneeds%lf.&agemeans "the address of age".scanfneeds to know where in memory to store the value.
Reading several values at once
#include <stdio.h>
int main(void) {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("Sum: %d, Product: %d\n", a + b, a * b);
return 0;
}
Output
Enter two numbers: 12 7 Sum: 19, Product: 84
The user can separate the numbers with spaces or by pressing Enter.
Reading a character
char answer;
printf("Continue? (y/n): ");
scanf(" %c", &answer); /* note the space before %c */
The space before %c tells scanf to skip any leftover whitespace, like the Enter key from a previous input. Without it, %c often reads that leftover newline instead of the user's letter.
Reading a word vs a whole line
%s reads only one word (it stops at a space). To read a full name, use fgets:
#include <stdio.h>
#include <string.h>
int main(void) {
char name[50];
printf("Full name: ");
fgets(name, sizeof name, stdin);
name[strcspn(name, "\n")] = '\0'; /* remove the newline fgets keeps */
printf("Hello, %s!\n", name);
return 0;
}
Output
Full name: Maria Clara Santos Hello, Maria Clara Santos!
fgets reads up to sizeof name - 1 characters, so it can never overflow the array. It keeps the Enter key's newline, and the strcspn line removes it.
Mixing scanf and fgets
After scanf("%d", ...), the newline stays in the input. A following fgets reads it as an empty line. Clear it first:
scanf("%d", &age);
while (getchar() != '\n'); /* discard the rest of the line */
fgets(name, sizeof name, stdin);
Checking that input worked
scanf returns the number of values it successfully read. Use it to catch bad input:
#include <stdio.h>
int main(void) {
int qty;
printf("Quantity: ");
if (scanf("%d", &qty) != 1) {
printf("That's not a number!\n");
return 1;
}
printf("Total: %.2f\n", qty * 2.5);
return 0;
}
Output
Quantity: sampu That's not a number!
A grade calculator
#include <stdio.h>
int main(void) {
double ww, pt, qe;
printf("Written works, performance tasks, quarterly exam: ");
if (scanf("%lf %lf %lf", &ww, &pt, &qe) != 3) {
printf("Please enter three numbers.\n");
return 1;
}
double grade = ww * 0.25 + pt * 0.50 + qe * 0.25;
printf("Quarterly grade: %.2f\n", grade);
return 0;
}
Output
Written works, performance tasks, quarterly exam: 88 92 85 Quarterly grade: 89.25
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04