- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Escape Sequences
![]() Share with a Friend |
C Programming - C Escape Sequences
C Escape Sequences
Escape sequences in C are used to represent special characters that cannot be typed directly in a string or character literal. They start with a backslash (\) followed by a specific character to represent non-printable or control characters.
Commonly Used Escape Sequences
| Escape Sequence | Description |
|---|---|
|
\n |
Newline (Moves cursor to the next line) |
|
\t |
Horizontal tab (Inserts a tab space) |
|
\b |
Backspace (Moves cursor one step back) |
|
\r |
Carriage return (Moves cursor to the beginning of the line) |
|
\\ |
Backslash (\) |
|
\' |
Single quote (') |
|
\" |
Double quote (") |
|
\0 |
Null character (End of a string) |
Advanced Escape Sequences
| Escape Sequence | Description |
|---|---|
|
\f |
Form feed (Moves to the next logical page) |
|
\v |
Vertical tab |
|
\a |
Alert or bell (Produces a beep sound if the system supports it) |
|
\? |
Question mark (?) |
|
\ddd |
Octal representation of a character (e.g., \101 for 'A') |
|
\xhh |
Hexadecimal representation of a character (e.g., \x41 for 'A') |
Usage in Strings and Characters
Escape sequences can be used in:
- Character Literals: Representing a single character.
- String Literals: Representing special characters within a string.
Examples:
C
#include <stdio.h>
int main() {
printf("Hello\nWorld"); // Outputs "Hello" and moves to the next line for "World"
printf("\tIndented Text"); // Outputs an indented line
printf("This is a backslash: \\"); // Outputs a backslash
printf("\nAlert sound\a"); // Produces a beep sound (if supported)
return 0;
}
Output:
Hello
World
Indented Text
This is a backslash: \
Alert sound
Escape Sequences for Non-Printable Characters
| Escape Sequence | Character Represented |
|---|---|
|
\n |
Line feed (LF) |
|
\t |
Horizontal tab (HT) |
|
\r |
Carriage return (CR) |
|
\f |
Form feed (FF) |
|
\v |
Vertical tab (VT) |
Examples of Octal and Hexadecimal Escape Sequences
- Octal Representation:
C
char ch = '\101'; // Represents 'A' (ASCII value 65 in octal)
printf("%c", ch); // Outputs: A
- Hexadecimal Representation:
C
char ch = '\x41'; // Represents 'A' (ASCII value 65 in hexadecimal)
printf("%c", ch); // Outputs: A
Key Points to Remember
- Escape sequences are treated as a single character.
- For example, \n is considered one character, not two.
- They are mostly used in string and character literals.
- The null character \0 is used to terminate strings in C.
- Hexadecimal and octal escape sequences allow representing ASCII values directly.
Practical Example
C
#include <stdio.h>
int main() {
printf("Special Characters in C:\n");
printf("Newline: \\n\n");
printf("Tab: \\t\tThis is indented.\n");
printf("Backslash: \\\\ \n");
printf("Double Quote: \\\" \n");
printf("Alert (Beep): \\a\a\n"); // May produce a beep sound
return 0;
}
Output:
Special Characters in C:
Newline: \n
Tab: \t This is indented.
Backslash: \
Double Quote: \"
Alert (Beep): \a
Escape sequences make it possible to work with special characters in a human-readable and programmatically efficient way.
