πŸ‡―πŸ‡΅ ζ—₯本θͺž | πŸ‡ΊπŸ‡Έ English
Advertisement

Lesson 4: printf & scanf

How to use printf and scanf in C. A complete reference of format specifiers.

printf — Display a String

printf displays a string (a sequence of characters) on the screen.
\n is a special escape sequence that means "newline."
printf("Hello World!\n");       // Print a string
printf("5 x 6 = %d\n", 30);    // Embed a number inside the string
printf("result = %d\n", value); // Display a variable's value

Format Specifiers — %d and %f

When printing a variable with printf, you must use the format specifier that matches the variable's type.
Integer → %d
int a = 5;
printf("%d", a);5

Width: %2d" 5", %4d" 5"
Floating point → %f
double b = 5.23;
printf("%f", b);5.230000

Precision: %5.2f" 1.52" (width 5, 2 decimals)
Mismatched type and format produce garbage output! Using %f for int or %d for double prints 0 or undefined values.

Format Specifier Tester

Result

scanf — Read Input from the Keyboard

scanf is a function that reads input from the keyboard. It essentially does the opposite of printf.
int a;
printf("Enter a value for a: ");
scanf("%d", &a);           // Don't forget the &!

double b;
printf("Enter a value for b: ");
scanf("%lf", &b);          // double uses %lf (NOT %f)

scanf Simulator

Type a value below and press "Enter" to see how the value gets stored in the variable.
Enter a value for a:
Value of variable a
(no input)
Result of printf("%d", a)

Common scanf Pitfalls

Pitfall 1: Don't forget &
scanf("%d", &a);
Without &, the program crashes.
Pitfall 2: double uses %lf
scanf("%lf", &b);
Use %lf, not %f.
Pitfall 3: Don't add extra text
Do not include prompt text or \n in the format string.
Pitfall 4: Beware consecutive scanf
Avoid multiple scanf calls in a row; put a printf between them.
Summary:
int → scanf: %d / printf: %d
double → scanf: %lf / printf: %f

Try It Yourself — printf & scanf

In this simulator, scanf is replaced by an input dialog.
io_demo.c
Output
Click "Run" to execute...
Advertisement

Related Lessons

Getting Started
Lesson 3: Variables
What is a variable in C? Learn int, double, and char with diagrams.
Getting Started
Lesson 2: Hello World
Write your first C program and learn the compile and run flow.
Reference
C Cheat Sheet
Quick reference for printf, operators, types, and more.
← Previous lesson
Lesson 3: Variables
Next →
C Cheat Sheet (Reference)

Review Quiz

Check your understanding of this lesson!

Q1. What is the output of printf("%d", 10);?

%d
10
10.000000

%d is the format specifier for integers. The value 10 is printed as-is.

Q2. Which scanf call correctly reads an integer?

scanf("%d", x);
scanf("%d", &x);
scanf("%d", *x);

scanf requires the address of the variable, so you must prefix it with & (the address-of operator).

Q3. What is the output of printf("%.2f", 3.14159);?

3.14
3.14159
3.1

%.2f prints a floating-point number with two digits after the decimal point. 3.14159 becomes 3.14.

Share this article
Share on X (Twitter) Share on Facebook