Step 3 of 5
Several lines
printf doesn't start a new line by itself. Each call continues exactly where the last one stopped, and only a \n moves the cursor to the start of the next line. So these two calls print on the same line:
#include <stdio.h>
int main(void) {
printf("Ready, ");
printf("set, ");
printf("go!\n");
printf("Next line\n");
return 0;
}
Ready, set, go!
Next line
Think of the output as a typewriter: the text goes where the cursor is, and \n is the carriage return. That means you can build one line from several calls, or print several lines from one call:
printf("one\ntwo\nthree\n"); // three lines from one call
Both styles are fine. Programs usually use one printf per line because it's easier to read and change.
The order is the order
Statements in main run top to bottom, one after another. If you swap two printf lines, their output swaps too. This "one step at a time, in order" idea is the foundation for everything that follows: later you'll learn to skip steps (if) and repeat them (loops).
Common mistakes
- Forgetting the
\nat the end, so the next thing printed is glued to the end of the line. - Putting
\nat the start instead of the end, which adds a blank line before the text.
Your turn: write a program that prints these three lines:
Counter-Strike
is a game
about aim