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

Horizontal tab `\t`

Horizontal tab \t

What you'll learn: How to use the backslash-t escape sequence to align text into neat, organized columns.

The invisible "jump" button

Imagine you are typing a document in a word processor like Microsoft Word or Google Docs. When you want to start a new paragraph, you hit Enter. But when you want to indent a line or create a list of data that lines up vertically, you reach for the Tab key on the left side of your keyboard.

In C, the \t character does exactly that. It is an "escape sequence" that represents a horizontal tab. Instead of just printing a single space, \t tells the computer to jump the cursor forward to the next "tab stop." Think of it like a train that doesn't stop at every single house along the track, but only at specific stations spaced at equal intervals.

Why use it?

When we print plain text to the console, everything usually squashes together. If you are trying to print a menu, a scoreboard, or a list of prices, the different lengths of words make everything look messy:

  • Apple: $1.00
  • Banana: $0.50
  • Watermelon: $5.00

Notice how the prices don't line up? Using \t fixes this by pushing the cursor to a consistent starting point for the next piece of information, regardless of how long the previous word was.

Seeing it in action

In the code below, we use \t to separate columns. Note how the backslash and the 't' stay inside the quotation marks, just like \n does.

#include <stdio.h>

int main() {
    // Printing a header for a simple table
    printf("Item\tColor\tPrice\n");
    
    // Printing rows of data
    printf("Apple\tRed\t$1.00\n");
    printf("Banana\tYellow\t$0.50\n");
    printf("Grape\tGreen\t$2.00\n");

    return 0;
}

When this runs, the computer calculates the space needed so that "Red," "Yellow," and "Green" all start at the exact same horizontal position. It turns a jumbled mess of text into a clean, professional-looking grid.

Key takeaway

Use \t inside a printf string to insert a tab space for aligning text into vertical columns.