Course contentsShow
← C
Lesson 44 of 1,2191. FoundationsFree lesson

Newline character `\n`

Newline character \n

What you'll learn: How to use the special escape sequence \n to move the cursor to a new line in your program's output.

The Problem: Clumped Text

By default, the printf function is very literal. If you call it three times in a row, it will jam all your text together on a single, long line. Computers don’t assume that a new line of code should result in a new line of text on the screen. If you want to start a fresh line, you have to tell the computer exactly where to "hit the Enter key."

The Solution: The Invisible Instruction

In C, we use a special symbol called the newline character, written as \n.

Think of \n like a hidden command tucked inside your sentences. Even though it looks like two characters (a backslash and an 'n'), the C compiler treats it as one single instruction that means: "Drop the cursor down to the start of the next line."

Seeing it in Action

If you were writing a shopping list, you wouldn't write "ApplesMilkBread" all in one word. You would put each item on its own row. In C, you do that by placing the \n right at the end of your text, inside the quotation marks.

#include <stdio.h>

int main() {
    // Without the newline, these would stay on the same line
    printf("Item 1: Apples\n");
    printf("Item 2: Milk\n");
    printf("Item 3: Bread\n");

    return 0;
}

The Typewriter Analogy

Imagine an old-fashioned manual typewriter. As you type, the carriage moves to the left. When you reach the end of a line, you have to push a lever to slide the paper up and move the carriage back to the start.

The \n character is that physical lever push. Without it, the typewriter would just keep typing off the edge of the paper or overtop of previous letters. You can even use multiple \n\n in a row to create a blank line between your text, just like hitting the Enter key twice in a word processor.

Key takeaway

Use \n inside your printf strings whenever you want to end the current line and start a new one.