Nearby lessons

25 of 124

C - Input

Reading input in C with scanf(), getchar() and fgets(). Learn why scanf needs the & operator, how to read a full line with spaces, and how to clear the leftover newline from the buffer.

scanf() — Reading Values

scanf() takes a format string, just like printf, plus the address of each variable to fill:

Example01
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int age;
6 float height;
7 
8 printf("Enter age: ");
9 scanf("%d", &age); // & = address of age
10 
11 printf("Enter height: ");
12 scanf("%f", &height);
13 
14 printf("You are %d years old and %.1f feet tall.\n", age, height);
15 return 0;
16}
Output
Enter age: 25
Enter height: 5.9
You are 25 years old and 5.9 feet tall.

Why the & is Required

C passes arguments by value — a function receives a copy. If scanf received a copy of age, it could only change the copy, and your variable would stay untouched.

The & operator hands over the variable's address instead, so scanf can write straight into the original storage.

In simple words: printf only needs to read your variable, so it takes the value. scanf needs to change it, so it needs the address.
Forgetting the & is the most common scanf bug. scanf("%d", age) compiles with a warning and then crashes at runtime, because scanf treats whatever number was in age as a memory address.

Reading Several Values at Once

One scanf can fill several variables. The user separates values with spaces, tabs or Enter:

Example03
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int day, month, year;
6 
7 printf("Enter date (dd mm yyyy): ");
8 scanf("%d %d %d", &day, &month, &year);
9 
10 printf("Date: %02d/%02d/%d\n", day, month, year);
11 return 0;
12}
Output
Enter date (dd mm yyyy): 7 8 2026
Date: 07/08/2026

The Problem with Strings and Spaces

scanf("%s", ...) stops at the first space, so it can never read a full name. Notice there is no & for a string — an array name is already an address:

Example04
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 char name[50];
6 
7 printf("Enter full name: ");
8 scanf("%s", name); // no & needed for arrays
9 
10 printf("Hello, %s!\n", name);
11 return 0;
12}
Output
Enter full name: John Smith
Hello, John!

fgets() — Reading a Whole Line

To capture spaces, use fgets(). It reads until Enter or until the buffer is full, whichever comes first — so it cannot overflow:

Example05
CCode Cell
1#include <stdio.h>
2#include <string.h>
3 
4int main()
5{
6 char name[50];
7 
8 printf("Enter full name: ");
9 fgets(name, sizeof(name), stdin);
10 
11 /* fgets keeps the newline - remove it */
12 name[strcspn(name, "\n")] = '\0';
13 
14 printf("Hello, %s!\n", name);
15 return 0;
16}
Output
Enter full name: John Smith
Hello, John Smith!

getchar() and the Leftover Newline

getchar() reads one character. The trap: after scanf("%d") the Enter key you pressed is still in the buffer, so the next character read is that \n, not your input.

Example06
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int age;
6 char grade;
7 
8 printf("Enter age: ");
9 scanf("%d", &age);
10 
11 printf("Enter grade: ");
12 scanf(" %c", &grade); // the SPACE before %c skips whitespace
13 
14 printf("Age %d, grade %c\n", age, grade);
15 return 0;
16}
Output
Enter age: 25
Enter grade: A
Age 25, grade A

Input Functions Compared

FunctionReadsSpaces?Safe?
scanf("%d")A numberStops at spaceYes for numbers
scanf("%s")One wordNoNo — can overflow
fgets()A full lineYesYes
getchar()One characterYesYes
gets()A full lineYesNever use it
Trainer's Note: gets() was removed from the C standard in C11 because it has no way to know how big your array is. Any program using it can be crashed by long input. Always reach for fgets().
📝 Key Takeaways
  • scanf("%d", &n) — the & is required for all types except strings.
  • scanf stops at the first whitespace, so it cannot read "John Smith".
  • Use fgets() to read a full line including spaces.
  • A leftover \n in the buffer makes the next %c read garbage.
  • Never use gets() — it cannot check the buffer size.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4