Escape Sequences in C
C programming language is widely used for system programming and low-level applications. In order to represent special characters in C programming language, we use a set of characters called escape sequences. Escape sequences are used to represent control characters, special characters, and other characters that cannot be represented directly in the program. In this article, we will discuss the escape sequences in C programming language, their usage, and examples.
-
Table of Contents
What are Escape Sequences?
An escape sequence is a set of characters that begins with a backslash () and is followed by one or more characters that have a special meaning in C programming language. Escape sequences are used to represent characters that cannot be typed directly in a string, character constant, or other literals.
-
Commonly Used Escape Sequences:
C programming language supports a wide range of escape sequences. Here are some commonly used escape sequences:
- \n – Newline character
- \t – Horizontal tab character
- \r – Carriage return character
- ‘ – Single quote character
- ” – Double quote character
- \0 – Null character
List of Escape Sequences in C
Escape Sequence | Meaning |
---|---|
\a | Alarm or Beep |
\b | Backspace |
\f | Form Feed |
\n | New Line |
\r | Carriage Return |
\t | Tab (Horizontal) |
\v | Vertical Tab |
\\ | Backslash |
\’ | Single Quote |
\” | Double Quote |
\? | Question Mark |
\nnn | octal number |
\xhh | hexadecimal number |
\0 | Null |
-
Examples of Escape Sequences:
Here are some examples of escape sequences in C programming language:
Printing a newline character:
// Using the escape sequence \n to print a newline character printf("Hello\nWorld!");
Output:
Hello
World!
Printing a horizontal tab character:
// Using the escape sequence \t to print a horizontal tab character printf("Hello\tWorld!");
Output:
Hello World!
Printing a backslash character:
// Using the escape sequence \\ to print a backslash character printf("\\");
Output:
\
Printing a single quote character:
// Using the escape sequence \' to print a single quote character printf("\'");
Output:
'
Printing a double quote character:
// Using the escape sequence \" to print a double quote character printf("\"");
Output:
"
Printing a null character:
// Using the escape sequence \0 to print a null character printf("%c", '\0');
Output:
-
Conclusion:
Escape sequences are an essential part of C programming language, and they are used to represent special characters in a program. In this article, we discussed the commonly used escape sequences in C programming language and provided examples of their usage. Understanding escape sequences is essential for any programmer who wants to work with strings, character constants, or other literals in C programming language.