Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What are Backslash character constants in C language?
A backslash (\) introduces an escape sequence that allows visual representation of non-graphic characters. Escape sequences are character combinations that have special meaning in C programming.
One of the most common escape sequences is the newline character (), which moves the cursor to the beginning of the next line.
Syntax
\character
Backslash Character Constants
The following table shows commonly used backslash character constants −
| Character | Meaning | Description |
|---|---|---|
| '\a' | Alert (Bell) | Produces an audible alert |
| '\b' | Backspace | Moves cursor back one position |
| '\f' | Form Feed | Moves to next page |
| ' ' |
Newline | Moves cursor to next line |
| '\r' | Carriage Return | Moves cursor to beginning of line |
| '\t' | Horizontal Tab | Moves cursor to next tab stop |
| '\v' | Vertical Tab | Moves cursor down one line |
| '\' | Backslash | Prints literal backslash |
| ''' | Single Quote | Prints single quote character |
| '"' | Double Quote | Prints double quote character |
| '\?' | Question Mark | Prints question mark character |
Example: Common Escape Sequences
Here's a program demonstrating various backslash character constants −
#include <stdio.h>
int main() {
printf("Hello\tWorld<br>"); /* Tab and newline */
printf("Line 1\nLine 2<br>"); /* Newlines */
printf("Quote: "Hello World"<br>"); /* Double quotes */
printf("Path: C:\Program Files\<br>"); /* Backslashes */
printf("Bell sound: \a<br>"); /* Alert sound */
printf("Single quote: 'C'<br>"); /* Single quote */
return 0;
}
Hello World Line 1 Line 2 Quote: "Hello World" Path: C:\Program Files\ Bell sound: Single quote: 'C'
Example: Formatting Output
This example shows how escape sequences improve output formatting −
#include <stdio.h>
int main() {
printf("Name\t\tAge\tGrade<br>");
printf("John\t\t25\tA<br>");
printf("Alice\t\t23\tB<br>");
printf("Bob\t\t24\tA<br>");
return 0;
}
Name Age Grade John 25 A Alice 23 B Bob 24 A
Key Points
- Escape sequences always start with a backslash (
\) followed by a character. - They are treated as single characters despite appearing as two characters in code.
- Use
\to print a literal backslash character. - The
character is the most commonly used escape sequence for line breaks.
Conclusion
Backslash character constants are essential for formatting output and including special characters in C strings. They provide a way to represent non-printable characters and control text formatting effectively.
