printf and scanf in C
On this page (14sections)
About this page
This is a focused tutorial in the C Data Input And Output group — format specifiers, field width, and scanf rules. The C Controls overview covers the same topic at chapter level; this page goes deeper with examples.
Introduction
printf and scanf are the most widely used formatted I/O functions in C, declared in <stdio.h>. printf writes formatted output to the screen; scanf reads formatted input from the keyboard.
printf — Formatted Output
#include <stdio.h>
int main(void) {
int age = 25;
float salary = 45000.50f;
char name[] = "Kumar";
printf("Name: %s\n", name);
printf("Age: %d\n", age);
printf("Salary: %.2f\n", salary);
return 0;
}
Sample Output
Name: Kumar
Age: 25
Salary: 45000.50
Common Format Specifiers
| Specifier | Type | Example |
|---|---|---|
%d or %i | int | printf("%d", n); |
%u | unsigned int | printf("%u", n); |
%f | float / double | printf("%f", x); |
%c | char | printf("%c", ch); |
%s | string (char*) | printf("%s", str); |
%p | pointer address | printf("%p", ptr); |
%% | literal % | printf("100%%"); |
Field Width and Precision
printf("%5d\n", 42); /* " 42" — right-aligned in 5 chars */
printf("%-5d|\n", 42); /* "42 |" — left-aligned */
printf("%.2f\n", 3.14159); /* "3.14" — 2 decimal places */
printf("%05d\n", 7); /* "00007" — zero-padded */
scanf — Formatted Input
#include <stdio.h>
int main(void) {
int age;
float height;
char name[50];
printf("Enter name: ");
scanf("%49s", name);
printf("Enter age and height: ");
scanf("%d %f", &age, &height);
printf("Hello %s, age %d, height %.1f\n", name, age, height);
return 0;
}
Sample Run
Enter name: Priya
Enter age and height: 22 5.4
Hello Priya, age 22, height 5.4
Important Rules for scanf
- Always pass addresses for numeric and char variables:
&age,&height. - For strings (
char[]), pass the array name directly (it is already an address). - Limit string length to prevent buffer overflow:
scanf("%49s", name)for a 50-char array. scanfreturns the number of successfully read items — check the return value in production code.
printf Special Characters
| Escape | Meaning |
|---|---|
\n | New line |
\t | Tab |
\\ | Backslash |
\" | Double quote |
See Escape Sequences in C for the full list.
Best Practices
- Prefer
fgetsoverscanf("%s")for reading full lines with spaces. - Never use
%swithout a width limit on fixed-size buffers. - Flush output before waiting for input if mixing printf and scanf in some environments.
- Match every format specifier to the correct argument type.
Common Mistakes
- Forgetting
&in scanf (scanf("%d", n)instead ofscanf("%d", &n)). - Using
%fin scanf for double — use%lffordoublein scanf (but%fin printf works for both). - Reading a char after a number without consuming the leftover newline.
Related Programs
Practice with Printf And Scanf Example Program.
Frequently Asked Questions
Why must I use & before variables in scanf?
scanf needs the memory address of the variable to store input. The & operator gives the address of a variable.
What happens if format specifiers do not match the arguments?
It causes undefined behaviour — always match %d with int, %f with float/double, %s with char array, etc.