Lesson 37 min read

Input & Output with printf and scanf

printf is your megaphone, scanf is your microphone

Megaphones and Microphones

Every program needs to talk to the outside world. In C, printf is your megaphone β€” it shouts formatted output to the screen. scanf is your microphone β€” it listens for input from the keyboard and stores it in your variables.

Both use format specifiers β€” little codes starting with % that tell C what type of data to expect. Get the specifier wrong, and you'll see garbage on screen or crash your program.

Format Specifiers β€” The Rosetta Stone

Here are the format specifiers you'll use constantly:

  • %d or %i β€” int (decimal integer)
  • %u β€” unsigned int
  • %f β€” float or double (in printf)
  • %lf β€” double (required in scanf)
  • %c β€” char (single character)
  • %s β€” string (char array)
  • %x β€” hexadecimal, %o β€” octal
  • %p β€” pointer address
  • %zu β€” size_t (result of sizeof)
  • %% β€” literal percent sign

printf Basics

#include <stdio.h>
int main() {
int age = 25;
float gpa = 3.87f;
char initial = 'J';
char name[] = "John";
printf("Name: %s\n", name);
printf("Initial: %c\n", initial);
printf("Age: %d\n", age);
printf("GPA: %f\n", gpa);
// Hex and octal
printf("255 in hex: %x\n", 255);
printf("255 in octal: %o\n", 255);
return 0;
}
Output
Name: John
Initial: J
Age: 25
GPA: 3.870000
255 in hex: ff
255 in octal: 377

Formatting Output β€” Width & Precision

You can control exactly how your output looks. Between the % and the specifier letter, you can add numbers for width (minimum characters) and precision (decimal places).

Formatted Output

#include <stdio.h>
int main() {
double price = 9.5;
int qty = 3;
// %.2f = 2 decimal places
printf("Price: $%.2f\n", price);
// %8d = right-aligned, 8 characters wide
printf("Qty: [%8d]\n", qty);
// %-8d = left-aligned, 8 characters wide
printf("Qty: [%-8d]\n", qty);
// %08d = zero-padded, 8 characters wide
printf("Code: %08d\n", 1234);
// Combining width and precision
printf("Pi: [%10.4f]\n", 3.14159);
return 0;
}
Output
Price: $9.50
Qty: [       3]
Qty: [3       ]
Code: 00001234
Pi: [    3.1416]

Escape Sequences

Special character combos that start with \:

  • \n β€” newline (move to next line)
  • \t β€” tab (horizontal indent)
  • \\ β€” literal backslash
  • \" β€” literal double quote inside a string
  • \' β€” literal single quote
  • \0 β€” null character (string terminator)

Escape Sequences in Action

#include <stdio.h>
int main() {
printf("Column1\tColumn2\tColumn3\n");
printf("-------\t-------\t-------\n");
printf("Alice\t95\tA\n");
printf("Bob\t87\tB+\n");
printf("\nShe said \"hello\"\n");
printf("Path: C:\\Users\\docs\n");
return 0;
}
Output
Column1	Column2	Column3
-------	-------	-------
Alice	95	A
Bob	87	B+

She said "hello"
Path: C:\Users\docs

scanf β€” Reading Input

The scanf function reads formatted input from the keyboard. The critical rule: you must pass the address of a variable using the & operator. This is because scanf needs to know where in memory to store the value.

The one exception: arrays (including strings) already are addresses, so you don't need & with %s.

scanf Basics

#include <stdio.h>
int main() {
int age;
double height;
char initial;
char name[50];
printf("Enter your first name: ");
scanf("%s", name); // No & for arrays!
printf("Enter your initial: ");
scanf(" %c", &initial); // Space before %c skips whitespace
printf("Enter your age: ");
scanf("%d", &age); // & is required!
printf("Enter your height (m): ");
scanf("%lf", &height); // %lf for double in scanf
printf("\n--- Your Info ---\n");
printf("Name: %s\n", name);
printf("Initial: %c\n", initial);
printf("Age: %d\n", age);
printf("Height: %.2f m\n", height);
return 0;
}
Output
Enter your first name: Alice
Enter your initial: A
Enter your age: 22
Enter your height (m): 1.68

--- Your Info ---
Name: Alice
Initial: A
Age: 22
Height: 1.68 m
Note: Two big scanf pitfalls:
1. Forgetting & β€” scanf("%d", age) instead of scanf("%d", &age) won't give you an error in many compilers, but your program will crash or corrupt memory. Always use & for non-array variables!
2. Buffer overflow with %s β€” If the user types a 100-character name into a char name[10], you get a buffer overflow. Use %49s (one less than array size) to limit input: scanf("%49s", name).

Reading Multiple Values

#include <stdio.h>
int main() {
int day, month, year;
printf("Enter date (dd/mm/yyyy): ");
scanf("%d/%d/%d", &day, &month, &year);
printf("Day: %02d\n", day);
printf("Month: %02d\n", month);
printf("Year: %d\n", year);
return 0;
}
Output
Enter date (dd/mm/yyyy): 15/03/2026
Day:   15
Month: 03
Year:  2026
Challenge

Quick check

What format specifier do you use for double in scanf?
← Operators & ExpressionsConditionals β†’