Skip to content
FollowtheDamnCode

Printing with printf

Format codes and escape sequences like \n and \t.

3 minute read

Big idea

printf prints text, and fills in values wherever you put a format code.

The idea

A format code like %d is a gap. printf fills each gap with the next value after the comma, in order:

printf("%d + %d = %d\n", 2, 3, 5);

prints 2 + 3 = 5.

An escape sequence is a backslash \ plus a letter, for characters you can't just type:

WriteGet
\nnew line
\ttab
\"a double quote
\\a backslash

Example

Try it — edit it and run it again
#include <stdio.h>

int main(void) {
    int score = 90;
    printf("Score:\t%d\n", score);
    printf("He said \"Great!\"\n");
    return 0;
}
output
Score:	90
He said "Great!"

Run and edit it in the app.

Watch it

Think of it like

A form with blanks: "Name: ____, Age: ____". The format codes are the blanks, and the values after the comma fill them in, left to right.

🕷️ Spidey-sense: printf is how your program talks back to the city. %d is a blank it fills in, and \n is how it presses Enter. Forget the \n and your next line crashes right into this one.

Common mistake

Writing a quote inside text without a backslash. The first " ends the text early.

printf("He said "Hi"");     // ✗
printf("He said \"Hi\"");   // ✓

Key terms

  • Format code — A placeholder like %d, %f, or %c that printf fills in.
  • Escape sequence — A backslash code like \n or \t.
  • \t — A tab space.

Check yourself

What does printf("%d + %d", 2, 3) print?

Which escape sequence adds a tab?

How do you print a double quote inside printf?