We already met one escape sequence in Hello World!. It was the \n at the end of the first printf. There are several escape sequences in C. Below is a table listing all of them.

Escape sequenceNameMeaningExample
\nNewlineMoves to the next lineHello\nWorld
\tHorizontal tabInserts a horizontal tabHello\tWorld
\vVertical tabInserts a vertical tabHello\vWorld
\bBackspaceMoves back one character positionHello\bWorld
\rCarriage returnMoves to the start of the current lineHello\rWorld
\fForm feedRepresents a form-feed characterHello\fWorld
\aAlertRequests an alert signal\a
\\BackslashPrints a backslash\\ → \
\'Single quoteRepresents a single quote\' → '
\"Double quoteRepresents a double quote\" → "
\?Question markRepresents a question mark\? → ?
\0Null characterRepresents a character with value zero'\0'
\oooOctal escapeRepresents a character using octal digits\101 → A
\xhhHexadecimal escapeRepresents a character using hexadecimal digits\x41 → A
\uXXXXUnicode escapeRepresents a Unicode character using four hexadecimal digits\u00A9 → ©
\UXXXXXXXXUnicode escapeRepresents a Unicode character using eight hexadecimal digits\U0001F600 → 😀

Difference Between \n and \r

The \n indicates a new line, while \r means a carriage return. But what does a carriage return mean in C? Consider this example code:

#include <stdio.h>
 
int main(void) {
  printf("ABCDEFGHIJKLMNO\r");
  printf("XYZ\n");
  return 0;
}

The output is

XYZDEFGHIJKLMNO

When \n is seen, it just moves the cursor to the beginning of a new line, vertically. \r, on the other hand, moves the cursor to the beginning of the current line, horizontally. Below is how it works.

Step 1: Original text
ABCDEFGHIJKLMNO

Step 2: After \r (cursor returns to beginning)
ABCDEFGHIJKLMNO
^
Step 3: Print XYZ (overwrites first 3 characters)
XYZDEFGHIJKLMNO
   ^

Previous: Hello World!

Next up: Data Types And Variables