tags: - printf - print - format - %d - output
Printing with printf¶
printf (short for "print formatted") prints text to the terminal. It is the main way your program communicates what is happening.
Basic usage¶
This prints Hello! followed by a new line. The \n is the newline character — it moves the cursor to the next line, just like pressing Enter. Without it, the next output would appear right after Hello! on the same line.
Format specifiers¶
printf can insert values into the output using format specifiers — placeholders starting with %.
%d — print an integer¶
Output:
The %d is replaced by the value of laps when the line runs.
%4d — print an integer, at least 4 digits wide¶
Output:
The 4 is the minimum field width. If the number is shorter than 4 digits, spaces are added on the left to pad it out. This keeps columns aligned when the value is changing:
Without %4d, the line width would jump around and be harder to read.
Multiple values in one line¶
You can have more than one %d in a format string. Add the corresponding variables after the format string, separated by commas, in the same order:
Output: