Nearby lessons

23 of 124

C - User Input (scanf)

printf sends values to the screen; scanf reads them back from the keyboard. This lesson covers scanf and the & that beginners always forget, reading single characters with getchar and putchar, and finishes with a complete program that takes three numbers and calculates simple interest.

scanf() — Taking Input

scanf("specifiers", &variables...) reads input from the keyboard. Important: you must put & before a variable (except for strings) so scanf knows where to store the value.

In simple words: scanf is the mirror image of printf — printf sends values to the screen, scanf receives them from the keyboard. Same specifiers, same order — but scanf needs a & before every variable (except strings).
Goal printf scanf
An integer printf("%d", age) scanf("%d", &age)
A float printf("%f", pay) scanf("%f", &pay)
A character printf("%c", ch) scanf("%c", &ch)
A string printf("%s", name) scanf("%s", name) — no &
Common Mistake: writing scanf("%d", age) without the &. The program still compiles, then either stores the value in a random place or crashes. If your input "does not work", check for a missing & first.
Trainer's Note: The & (address of) operator is essential in scanf: scanf("%d", &age) stores the input at age's memory location. Strings are the one exception — an array name already is an address, so %s takes no &. This will make full sense after the Pointers chapter.
Example01
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 int age;
6 float salary;
7 
8 printf("Enter your age: ");
9 scanf("%d", &age);
10 
11 printf("Enter your salary: ");
12 scanf("%f", &salary);
13 
14 printf("Age = %d, Salary = %.2f\n", age, salary);
15 
16 return 0;
17}
Output
Enter your age: 20
Enter your salary: 5000.50
Age = 20, Salary = 5000.50

Reading and Writing Single Characters

When you only need one character, scanf is more machinery than you need:

  • getchar() — reads one character from the keyboard.
  • putchar(c) — prints one character.
In simple words: getchar and putchar are the single-character versions of scanf and printf. No format string, no & — just one character in, one character out.
Trainer's Note: The classic textbook also uses getch() and getche() (from conio.h) — but those are Windows-only functions and not part of standard C. Modern practice is to use getchar() (standard) or the safer fgets() for strings, which we see in Chapter 8.
Example02
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 char ch;
6 
7 printf("Enter one character: ");
8 ch = getchar();
9 
10 printf("You typed: ");
11 putchar(ch);
12 printf("\n");
13 
14 return 0;
15}
Output
Enter one character: A
You typed: A

A Complete Example — Calculate Simple Interest

This program puts the whole chapter together: it declares variables, reads three values with one scanf, calculates, and prints the result to two decimals.

One scanf can read several values at once — "%f %f %f" reads three floats, and the user separates them with spaces or newlines. Notice each variable still gets its own &.

In simple words: the four parts of main() from the Program Structure lesson are all here — declare (float p, r, t, si;), input (scanf), process (si = ...), output (printf). Almost every C program you write will follow this shape.
Example03
CCode Cell
1#include <stdio.h>
2 
3int main()
4{
5 float p, r, t, si;
6 
7 printf("Enter principal, rate, time: ");
8 scanf("%f %f %f", &p, &r, &t);
9 
10 si = (p * r * t) / 100;
11 
12 printf("Simple Interest = %.2f\n", si);
13 
14 return 0;
15}
Output
Enter principal, rate, time: 10000 8 2
Simple Interest = 1600.00
📝 Key Takeaways
  • printf() outputs; scanf() inputs (from stdio.h).
  • Format specifiers: %d int, %f float, %.2f two decimals, %c char, %s string.
  • Escape sequences: \n new line, \t tab, \\ backslash, %% percent.
  • scanf needs & before variables (except strings).
  • getchar()/putchar() handle one character.
  • getch/getche are Windows-only; prefer standard getchar.

🧠 Test Your Knowledge

5 Questions
Progress: 0 / 5