Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 5 of 10
printf & scanf - Formatted I/O
`printf` and `scanf` are the primary I/O functions in C. Both work with format strings containing conversion specifiers (`%d`, `%s`, `%f`, etc.). `printf` writes formatted output to `stdout`; `scanf` reads formatted input from `stdin`. Both are variadic - they accept a variable number of arguments. Using the wrong format specifier is **undefined behaviour** in C.
printf - All Important Format Specifiers
#include <stdio.h>
int main(void) {
int i = -42;
unsigned u = 255;
long l = 1000000L;
float f = 3.14f;
double d = 3.14159265358979;
char c = 'A';
char *s = "hello";
void *p = &i;
/* Integer formats */
printf("%d
", i); /* signed decimal: -42 */
printf("%i
", i); /* same as %d */
printf("%u
", u); /* unsigned decimal: 255 */
printf("%o
", u); /* octal: 377 */
printf("%x
", u); /* hex lowercase: ff */
printf("%X
", u); /* hex uppercase: FF */
printf("%ld
", l); /* long int */
printf("%lld
", (long long)l); /* long long */
/* Floating-point formats */
printf("%f
", d); /* fixed: 3.141593 */
printf("%.2f
", d); /* 2 decimal places: 3.14 */
printf("%e
", d); /* scientific: 3.141593e+00 */
printf("%g
", d); /* shorter of %f or %e */
/* Other */
printf("%c
", c); /* character: A */
printf("%s
", s); /* string: hello */
printf("%p
", p); /* pointer address */
printf("%%
"); /* literal percent sign */
/* Width and alignment */
printf("|%10d|
", 42); /* right-align in 10 chars */
printf("|%-10d|
", 42); /* left-align */
printf("|%010d|
", 42); /* zero-pad: 0000000042 */
printf("|%+d|
", 42); /* always show sign: +42 */
return 0;
}scanf - Reading Input Safely
#include <stdio.h>
int main(void) {
int age;
double weight;
char name[64]; /* fixed-size buffer for string input */
/* scanf returns the number of items successfully read */
printf("Enter your age: ");
if (scanf("%d", &age) != 1) { /* & = address-of - REQUIRED */
fprintf(stderr, "Error: invalid integer input
");
return 1;
}
printf("Enter your weight (kg): ");
if (scanf("%lf", &weight) != 1) { /* %lf for double with scanf */
fprintf(stderr, "Error: invalid float input
");
return 1;
}
/* SAFE string reading: %63s limits to 63 chars + null terminator */
/* Never use: scanf("%s", name) ← buffer overflow! */
printf("Enter your name: ");
if (scanf("%63s", name) != 1) {
fprintf(stderr, "Error: invalid string input
");
return 1;
}
printf("Name: %s, Age: %d, Weight: %.1f kg
", name, age, weight);
return 0;
}Common Mistakes
- Forgetting `&` in `scanf` for scalars - `scanf("%d", age)` passes the VALUE of age (garbage or 0), not its address. The function overwrites whatever memory is at that address - undefined behaviour, usually a crash.
- Using `%s` in `scanf` without a width limit - `scanf("%s", buf)` reads until whitespace with NO length limit. If the user types more characters than the buffer can hold, the stack is corrupted. Always: `scanf("%63s", buf)` where 63 = buffer size - 1.
- Using `%d` to scan a `double` - `scanf("%d", &d)` where `d` is `double` is undefined behaviour. Use `%lf` for `double` in scanf (but `%f` in printf for both float and double).
Tip
Tip
Practice printf scanf Formatted IO in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
printf formats values to stdout while scanf parses formatted tokens from stdin into memory addresses
Practice Task
Note
Practice Task - (1) Write a working example of printf scanf Formatted IO from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with printf scanf Formatted IO is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- `printf` and `scanf` are the primary I/O functions in C.
- Forgetting `&` in `scanf` for scalars - `scanf("%d", age)` passes the VALUE of age (garbage or 0), not its address. The function overwrites whatever memory is at that address - undefined behaviour, usually a crash.
- Using `%s` in `scanf` without a width limit - `scanf("%s", buf)` reads until whitespace with NO length limit. If the user types more characters than the buffer can hold, the stack is corrupted. Always: `scanf("%63s", buf)` where 63 = buffer size - 1.
- Using `%d` to scan a `double` - `scanf("%d", &d)` where `d` is `double` is undefined behaviour. Use `%lf` for `double` in scanf (but `%f` in printf for both float and double).
Finished this lesson?