Loading lessons...
Printing Text with printf
The workhorse of C output is printf, which lives in the <stdio.h> header.
One line
c
#include <stdio.h>
int main() {
printf("Hello World!");
return 0;
}
- This prints: Hello World!
Two printf statements, same line
Both printf statements write with no break in between, so the console shows one line:
c
printf("Hello World!");
printf(" I am learning C");
Output:
### Hello World! I am learning C
Printing on multiple lines
Use the newline character `
` inside the string:
c
printf("Hello World!
");
printf("I am learning C.
");
Output:
Hello World!
I am learning C.
The golden rules
- Put text inside double quotes
"...". - End each statement with a semicolon.
- Without `
`, everything stays on the same line.
TL;DR
printf("text")prints text.- Two
printflines without `
` share one console line.
- `
` inside a string jumps to a new line.
- Keep all print calls in order; output appears top-to-bottom.