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);
}
OutputHello
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);
}
\\ - Prints a single backslash (\)
C++
#include <stdio.h>
int main(void)
{
// \\ prints a single backslash (\)
printf("Hello\\GFG");
return (0);
}
\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;
}
\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;
}
OutputHello!
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;
}
\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;
}
Escape Sequence List
The table below lists some common escape sequences in C language.
| Escape Sequence | Name | Description |
|---|
| \a | Alarm or Beep | It is used to generate a bell sound in the C program. |
| \b | Backspace | It is used to move the cursor one place backward. |
| \f | Form Feed | It is used to move the cursor to the start of the next logical page. |
| \n | New Line | It moves the cursor to the start of the next line. |
| \r | Carriage Return | It moves the cursor to the start of the current line. |
| \t | Horizontal Tab | It inserts some whitespace to the left of the cursor and moves the cursor accordingly. |
| \v | Vertical Tab | It is used to insert vertical space. |
| \\ | Backslash | Use to insert backslash character. |
| \' | Single Quote | It is used to display a single quotation mark. |
| \" | Double Quote | It is used to display double quotation marks. |
| \? | Question Mark | It is used to display a question mark. |
| \ooo | Octal Number | It is used to represent an octal number. |
| \xhh | Hexadecimal Number | It represents the hexadecimal number. |
| \0 | NULL | It 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.
Explore
C Basics
Arrays & Strings
Pointers and Structures
Memory Management
File & Error Handling
Advanced Concepts