Nearby lessons
24 of 124C - Output
Printing to the screen in C using printf(), puts() and putchar(). Learn format specifiers, field width, decimal precision and why output sometimes appears in the wrong order.
The Three Output Functions
All three live in <stdio.h> and each has one job:
| Function | Prints | Adds newline? | Formats values? |
|---|---|---|---|
printf() | Anything | No | Yes |
puts() | A string only | Yes | No |
putchar() | One character | No | No |
printf when you need to mix text with variables, puts for a plain message, and putchar for a single character.printf() — Formatted Output
Inside the quotes you write ordinary text plus format specifiers that start with %. Each specifier is replaced, in order, by the values you list after the comma:
The Specifiers You Need Most
| Specifier | Type | Example output |
|---|---|---|
%d | int | 25 |
%f | float / double | 5.900000 |
%c | char | A |
%s | string | Rahul |
%lf | double | 3.141593 |
%u | unsigned int | 4000000000 |
%% | a literal % sign | % |
printf("%d", 3.14) does not print 3 — it reinterprets the float's bits as an integer and prints nonsense. The specifier must match the value's type.Controlling Decimals and Width
Between the % and the letter you can add a width and a precision. This is how you line up columns and print money correctly:
puts() and putchar()
puts() is shorter than printf for plain messages because it supplies the newline itself:
Printing Special Characters
Some characters cannot be typed directly inside quotes, so you write an escape sequence instead:
Why Output Sometimes Appears Late
C does not write to the screen immediately. It collects characters in a buffer and flushes them when it sees a newline, when the buffer fills, or when the program ends.
That is why a prompt without \n can appear after you have already typed your answer:
- printf() prints formatted output; it needs a format specifier per value.
- puts() prints a string and adds a newline automatically.
- putchar() prints exactly one character.
- Use %.2f to print two decimal places.
- printf does not add a newline — you must write \n yourself.