In C programming, outputting data to the screen is an incredibly common task. Whether you want to display a welcome message, show the result of a calculation, or debug your application, you need to know how to print text to the console.
The primary function used for outputting text in C is the printf() function.
printf() FunctionThe printf() function outputs text exactly as you format it. To output a simple string of text, you place the text inside double quotes "" within the parentheses.
#include <stdio.h>int main() { printf("Hello, World!"); printf("I am learning C."); return 0; }
If you run the example above, you might expect the output to be on two separate lines. However, the output will actually look like this:
Hello, World!I am learning C.
Why? Because the printf() function does not automatically add a line break at the end of your text. It prints exactly what you tell it to, right next to the previous output.
\n)To move the cursor to a new line, you must explicitly use the newline character: \n.
When the compiler encounters \n inside a printf() string, it understands it as a command to start a new line, rather than printing the literal characters '' and 'n'.
#include <stdio.h>int main() { printf("Hello, World!\n"); printf("I am learning C.\n"); printf("This will be on a third line."); return 0; }
You can also use multiple \n characters together to create blank lines:
printf("Hello!\n\n\nHow are you?");
// This creates two blank lines before printing "How are you?"
The printf() function is powerful because it doesn't just print static text. It can print the values of variables dynamically. To do this, we use format specifiers.
A format specifier starts with a percentage sign % followed by a character indicating the type of data to be printed.
%d or %i - Used for integers (whole numbers).%f - Used for floating-point numbers (decimals).%c - Used for a single character.%s - Used for strings (text).#include <stdio.h>int main() { int myAge = 25; char myGrade = 'A';
// The %d will be replaced by the value of myAge printf("I am %d years old.\n", myAge);
// The %c will be replaced by the value of myGrade printf("My exam grade is %c.\n", myGrade);
return 0; }
We will dive much deeper into variables and data types in the upcoming chapters!
The newline character \n is called an escape sequence. It "escapes" the normal string interpretation to perform a special action. There are a few other handy escape sequences in C:
| Escape Sequence | Description | Use Case |
|---|---|---|
\n |
New Line | Moves text to the next line. |
\t |
Horizontal Tab | Inserts a large gap, useful for aligning text into columns. |
\\ |
Backslash | Prints a literal backslash \. |
\" |
Double Quote | Prints a literal double quote " inside a string. |
#include <stdio.h>int main() { // Using tab to align data printf("Name\tAge\n"); printf("John\t22\n");
// Printing quotes requires " printf("They call him "The Programmer".\n");
return 0; }
Which escape sequence is used to force text to continue on a new line?