C Output (Print Text)
In C, text is printed with the printf function from <stdio.h>. The name stands for print formatted: besides plain text, it can insert numbers and other values into the output. You will learn the formatting part in the next lessons; here we focus on printing text and controlling lines.
Printing text
Put the text you want to show inside double quotes. Text in double quotes is called a string literal.
#include <stdio.h>
int main(void) {
printf("Welcome to C programming!");
return 0;
}Welcome to C programming!
Note: Strings in C always use double quotes
"...". Single quotes'A'are for a single character, which is a different type.
New lines
printf does not add a line break on its own. If you call it twice, the second text continues on the same line:
#include <stdio.h>
int main(void) {
printf("Hello ");
printf("there.");
printf("No line breaks yet!");
return 0;
}Hello there.No line breaks yet!
To end a line, write the newline escape sequence \n inside the string. You can put it anywhere — at the end, in the middle, or several in a row to create empty lines.
#include <stdio.h>
int main(void) {
printf("Line one\n");
printf("Line two\nLine three\n");
printf("\n");
printf("After an empty line\n");
return 0;
}Line one
Line two
Line three
After an empty line
puts — the simpler alternative
puts prints a string and automatically adds a newline. It cannot insert values, but for plain messages it is short and clear.
#include <stdio.h>
int main(void) {
puts("puts adds the newline for you.");
puts("So each call is its own line.");
printf("printf needs an explicit \\n.\n");
return 0;
}puts adds the newline for you.
So each call is its own line.
printf needs an explicit \n.
Notice \\n in the last string: a backslash followed by another backslash prints a single real backslash, so the output shows the characters \n instead of a line break. More escape sequences are covered in C Special Characters.
Comparing output functions
| Function | Adds newline? | Can insert values? | Example |
|---|---|---|---|
printf |
No | Yes (%d, %s, …) |
printf("Age: %d\n", 30); |
puts |
Yes | No | puts("Done"); |
putchar |
No | Prints one character | putchar('A'); |
Printing numbers — a preview
You cannot pass a number directly as the first argument of printf. Instead, you write a format specifier such as %d in the text, and give the value as an extra argument:
#include <stdio.h>
int main(void) {
printf("Today I wrote %d programs.\n", 3);
printf("%d + %d = %d\n", 2, 5, 2 + 5);
putchar('O');
putchar('K');
putchar('\n');
return 0;
}Today I wrote 3 programs.
2 + 5 = 7
OK
Each %d is replaced by the next argument, in order. Format specifiers get their own lesson soon.
Tip: Output to the terminal is often buffered and only appears when a newline is printed or the program ends. Ending your lines with
\nkeeps output appearing in the order you expect.
Test yourself
What is printed by printf("A"); printf("B\n");?
- A and B on separate lines
- AB on one line
- A B
- Nothing
Which function automatically adds a newline after the text?
- printf
- putchar
- puts
How do you print the number 42 with printf?
- printf(42);
- printf("%d", 42);
- printf("42", %d);
- printf(%d, 42);