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

Lesson 4: printf

How to use C printf with format specifiers. Interactive tester for %d / %f / %s / width / precision.

πŸ“– What to learn on this page
βœ… Must-know essentials
  • printf("...%d...\n", x); prints a value
  • %d=int, %f=double, %c=char, %s=string
  • scanf("%d", &x); reads input β€” don't forget &
  • \n is newline
⭐ Read if you have time
  • Width/precision (%5d, %.2f)
  • %x (hex), %o (octal)
  • scanf return value and error handling

printf — Print a String

printf prints a string (a sequence of characters) to the screen.
\n is an escape sequence meaning "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 you print a variable with printf, the format specifier has to match 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)
Type/format mismatches print garbage! Using %f for an int or %d for a double produces zero or undefined values.

Printing Multiple Variables in One printf

Add more % specifiers to the format string and list the variables in the same order after the comma. The first % pairs with the first variable, the second % with the second, and so on.
int age = 25;
double h = 170.5;

printf("Age %d, height %.1f cm\n",
    age, h);

// Output: Age 25, height 170.5 cm
Order is everything
First %d ← age, second %.1f ← h. They're filled in the order you list them.
⚠️ Counts must match
Number of % specifiers = number of arguments after the comma. A mismatch prints garbage.
πŸ’‘ You can pass expressions too
Arguments aren't limited to variables β€” you can pass whole expressions and the result gets formatted directly.
printf("%d + %d = %d\n", a, b, a+b); β†’ 3 + 5 = 8

Controlling Decimal Places for double

By default, %f prints six digits after the decimal point. So 3.14 comes out as 3.140000 β€” a lot of noise. The precision specifier lets you tame that.
Basic form: %.Nf β€” N digits after the decimal point
Format Meaning Output for b = 3.14159
%fDefault (6 decimal digits)3.141590
%.0fInteger part only (rounded)3
%.1f1 decimal digit (rounded)3.1
%.2f2 decimal digits (common β€” currency, %)3.14
%.4f4 decimal digits (physics/science)3.1416
%8.2fWidth 8, right-aligned, 2 decimal digits"    3.14"
%-8.2fWidth 8, left-aligned, 2 decimal digits"3.14    "
%08.2fWidth 8, zero-padded, 2 decimal digits"00003.14"
β€» In %W.Nf, W is the total width (including the decimal point) and N is the number of digits after the decimal point. You can omit either one (%.3f, %10f).
βœ‚οΈ Rounded, not truncated
double x = 3.145;
printf("%.2f", x); β†’ 3.15 (rounded up)

double y = 3.144;
printf("%.2f", y); β†’ 3.14 (rounded down)

Extra digits are rounded, not chopped off, so the printed value can differ slightly from what's actually stored.
πŸ“Š Use %e or %g for very large/small numbers
double v = 0.00000123;
printf("%.2f", v); β†’ 0.00 (information is lost!)
printf("%.2e", v); β†’ 1.23e-06 (scientific notation)
printf("%g", v); β†’ 1.23e-06 (whichever form is shorter)

For extreme values, %e or %g preserves the meaningful digits.

Format Specifier Tester

Result

Try It Yourself — printf & scanf

In this simulator, scanf is replaced by a prompt dialog.
io_demo.c
Output
Click "Run" to execute...
πŸ’‘ A few things to try

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 lesson →
Lesson 6: Quiz (Variables & printf)

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, so 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 needs the variable's address, so you prefix the variable 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, so 3.14159 becomes 3.14.

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