Open In App

Escape Sequence in C

Last Updated : 01 Nov, 2025
Comments
Improve
Suggest changes
265 Likes
Like
Report

Escape sequences are special character representations in strings, used to denote characters that cannot be entered directly from the keyboard or that carry a specific control function. They start with a backslash \ followed by a character (e.g., \n, \t).

Different escape sequences represent different characters but the output is dependent on the compiler you are using.

Escape Sequence in C Examples

The following are the escape sequence examples that demonstrate how to use different escape sequences in C language.

\n - Prints a new line

C
#include <stdio.h>

int main(void)
{
    // Here we are using \n, which is a new line character.
    printf("Hello\n");
    printf("GeeksforGeeks");
    
    return (0);
}

Output
Hello
GeeksforGeeks

\t - Prints a tab

C++
#include <stdio.h>

int main(void)
{
    // \t will provide a tab space between two words.
    printf("Hello \t GFG");
    
    return (0);
}

Output
Hello 	 GFG

\\ - Prints a single backslash (\)

C++
#include <stdio.h>

int main(void)
{
    // \\ prints a single backslash (\)
    printf("Hello\\GFG");
    
    return (0);
}

Output
Hello\GFG

\b - Moves the cursor one position back

C
#include <stdio.h>

int main(void)
{
    // \b (backspace) character moves the cursor one position back
    printf("Hello \b\b\b\b\b\bHi Geeks");
    
    return (0);
}


Output

Hi Geeks

\' and \'' - Prints single (') and double (") quote.

C++
#include <stdio.h>

int main(void)
{
    // \' prints a single quote (')
    printf("\' Hello Geeks\n");

    // \" prints a double quote (")
    printf("\" Hello Geeks");

    return 0;
}

Output
' Hello Geeks
" Hello Geeks

\v - Prints a vertical tab

C
#include <stdio.h>

int main(void)
{
    // \v prints a vertical tab and moves the cursor down to the next vertical tab position.
    printf("Hello friends\v");

    printf("Welcome to GFG");

    return (0);
}


Output

Hello friends
Welcome to GFG

\r - Moves the cursor to the beginning of the current line.

C
#include <stdio.h>

int main(void)
{
    // \r Moves the cursor to the beginning of the current line
    printf("Hello   Geeks \rGeeksfor");
    
    return (0);
}


Output

GeeksforGeeks

\? - Prints a question mark

C
#include <stdio.h>

int main(void)
{
    // \? is used to print a question mark (?)
    printf("\?\?!\n");

    return 0;
}

Output
??!

\a - Produces a beep/alert sound.

C++
#include <stdio.h>

int main(void)
{
    // \a triggers a sound or system beep in the console
    printf("Hello!\a\n");
    printf("This is a beep\a\n");

    return 0;
}

Output
Hello!
This is a beep

\ooo - Represents a character using its octal value.

C
#include <stdio.h>

int main(void)
{
    // \ooo Represents a character using its octal value (0-7)
    char *s = "A\072\065";
    printf("%s", s);

    return 0;
}

Output
A:5

\xhh - Represents a character using its hexadecimal value.

C
#include <stdio.h>

int main(void)
{
    // \xhh Represents a character using its hex value (0-9, a-f, A-F)
    char *s = "B\x4a";
    printf("%s", s);

    return 0;
}

Output
BJ

Escape Sequence List

The table below lists some common escape sequences in C language.

Escape SequenceNameDescription
\aAlarm or BeepIt is used to generate a bell sound in the C program.
\bBackspaceIt is used to move the cursor one place backward.
\fForm FeedIt is used to move the cursor to the start of the next logical page.
\nNew LineIt moves the cursor to the start of the next line.
\rCarriage ReturnIt moves the cursor to the start of the current line.
\tHorizontal TabIt inserts some whitespace to the left of the cursor and moves the cursor accordingly.
\vVertical TabIt is used to insert vertical space.
\\BackslashUse to insert backslash character.
\'Single QuoteIt is used to display a single quotation mark.
\"Double QuoteIt is used to display double quotation marks.
\?Question MarkIt is used to display a question mark.
\oooOctal NumberIt is used to represent an octal number.
\xhhHexadecimal NumberIt represents the hexadecimal number.
\0NULLIt represents the NULL character.

Out of all these escape sequences, \n and \0 are used the most. In fact, escape sequences like \f, \a,  are not even used by programmers nowadays.


Escape Sequence in C
Visit Course explore course icon
Article Tags :

Explore