Escape Characters in C Language
Escape sequences are special characters.
They are used inside strings or character constants.
They start with a backslash
\ and represent actions such as
a new line, tab space, etc.
| Escape Character | Meaning | Example Output |
|---|---|---|
| \n | New line (moves the cursor to the next line) | New Line |
| \t | Horizontal tab (adds tab space) | Tab Space |
| \b | Backspace (removes the previous character) | Back\bspace |
| \r | Carriage return (moves the cursor to the beginning of the line) | Carriage\rReturn |
| \f | Form feed / new page (page break – rarely used) | Form\fFeed |
| \v | Vertical tab (moves the cursor down) | Vertical\vTab |
| \a | Alert / beep (produces a sound) | Alert\aBeep |
| \\ | Backslash (prints \) | \ |
| \' | Single quote (prints ') | ' |
| \" | Double quote (prints ") | " |
| \? | Question mark (used to avoid trigraph conflicts) | ? |
| \0 | Null character (end of string) | \0 |
Numeric Escape Sequences in C Language
Numeric escape sequences represent characters using numeric values.
They are mainly of two types.
- Octal Escape Sequence
- Hexadecimal Escape Sequence
1. Octal Escape Sequence
- Syntax: \ooo
- Here,
ooois an octal number (base 8). - It represents an ASCII value in octal form.
- Range: 1 to 3 octal digits (0–7).
Example
#include<stdio.h>
int main() {
printf("Programming Language \103"); // \103 = 'C' (octal 103 = decimal 67)
return 0;
}
Output:
Programming Language C
2. Hexadecimal Escape Sequence
- Syntax: \xhh
- Here,
hhis a hexadecimal number (base 16). - It represents an ASCII value in hexadecimal form.
- Range: Any number of hex digits (0–9, a–f, A–F).
Example
#include<stdio.h>
int main() {
printf("Programming Language \x43"); // \x43 = 'C' (hex 43 = decimal 67)
return 0;
}
Output:
Programming Language C
Null Character (Special Case)
- Syntax: \0
- It indicates the end of a string.
- In C, strings always end with a null character.
Example
#include<stdio.h>
int main() {
char str[] = "Programming\0 Language";
printf("%s", str);
return 0;
}
Output:
Programming
(The word "Language" is not printed because \0 marks the end of the string.)