- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- 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 - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
Escape Sequence in C
Escape Sequence in C
An escape sequence in C is a literal made up of more than one character put inside single quotes. Normally, a character literal consists of only a single character inside single quotes. However, the escape sequence attaches a special meaning to the character that appears after a backslash character (\).
The \ symbol causes the compiler to escape out of the string and provide meaning attached to the character following it.
Look at \n as an example. When put inside a string, the \n acts as a newline character, generating the effect of pressing the Enter key. The following statement −
printf(" Hello \n World ");
Will produce this output −
Hello World
The new line is an unprintable character. The \n escape sequence is useful to generate its effect. Similarly, the escape sequence \t is equivalent to pressing the Tab key on the keyboard.
An escape sequence is a sequence of characters that does not represent itself when used inside a character or string literal but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly.
All Escape Sequences in C
In C, all escape sequences consist of two or more characters, the first of which is the backslash \ (called the "Escape character"); the remaining characters have an interpretation of the escape sequence as per the following table.
Here is a list of escape sequences available in C −
Escape sequence | Meaning |
---|---|
\\ | \ character |
\' | ' character |
\" | " character |
\? | ? character |
\a | Alert or bell |
\b | Backspace |
\f | Form feed |
\n | Newline |
\r | Carriage return |
\t | Horizontal tab |
\v | Vertical tab |
\ooo | Octal number of one to three digits |
\xhh . . . | Hexadecimal number of one or more digits |
Let us understand how these escape sequences work with the help of a set of examples.
Newline Escape Sequence (\n)
The newline character, represented by the escape sequence \n in C, is used to insert the effect of carriage return on the output screen. You would use this escape sequence to print text in separate lines and improve the readability of output.
Example
Take a look at the following example −
#include <stdio.h> int main(){ printf("Hello.\nGood morning.\nMy name is Ravi"); }
Output
On running this code, you will get the following output −
Hello. Good morning. My name is Ravi
Tab Escape Sequence (\t)
The tab character (\t) represents the Tab key on the keyboard. When the tab character is encountered in a string, it causes the cursor to move to the next horizontal tab stop. Horizontal tab stops are usually set at intervals of eight characters.
Example
Take a look at the following example −
#include <stdio.h> int main(){ printf("Name:\tRavi\tMarks:\t50"); }
Output
Run the code and check its output −
Name: Ravi Marks: 50
Backslash Escape Sequence (\\)
To add backslash character itself as a part of a string, it must precede by another backslash. First backslash escapes out of the string, and the second one takes the effect.
Example
Take a look at the following example −
#include <stdio.h> int main(){ printf("Directory in Windows: C:\\users\\user"); }
Output
On running this code, you will get the following output −
Directory in Windows: C:\users\user
Double and Single Quotes Escape Sequences (\" and \')
These characters have a special meaning in C since " and ' symbols are used for the representation of a character literal and a string literal respectively. Hence, to treat these characters as a part of the string, they must be escaped with an additional backslash preceding them.
Example
Take a look at the following example −
#include <stdio.h> int main(){ printf("Welcome to \"TutorialsPoint\"\n"); printf ("\'Welcome\' to TutorialsPoint"); }
Output
Run the code and check its output −
Welcome to "TutorialsPoint" 'Welcome' to TutorialsPoint
Backspace Escape Sequence (\b)
The escape sequence "\b", represents the backspace character. It is used erase a character or a specific portion of a text that has already been printed on the screen.
Example
Check the following example code −
#include <stdio.h> int main(){ printf("Welcome to\b TutorialsPoint"); }
Output
Run the code and check its output −
Welcome t TutorialsPoint
Note that o from to has been erased.
C also has a \r escape sequence. The newline escape sequence (\n) moves the cursor to the beginning of the next line, while the carriage return escape sequence (\r) moves the cursor to the beginning of the current line.
Octal Number Escape Sequence (\ooo)
This escape sequence is used for Octal numbers of one to three digits. An octal escape sequence is a backslash followed by one, two, or three octal digits (0-7). It matches a character in the target sequence with the value specified by those digits.
Example
Take a look at the following example −
#include <stdio.h> int main(){ printf("%c", '\141'); return 0; }
Output
When you run this code, it will produce the following output −
a
Hexadecimal Number Escape Sequence (\xhh)
A hexadecimal escape sequence is a backslash followed by the letter "x" followed by two hexadecimal digits (0-9a-fA-F). It matches a character in the target sequence with the value specified by the two digits.
Example
Take a look at the following example −
#include <stdio.h> int main(){ printf("%c", '\x41'); return 0; }
Output
Here, you will get the following output −
A
Alert or Bell Number Escape Sequence (\a)
The escape sequence \a represents the alert or bell character. When executed, it produces a sound or visual alert depending on the terminal or console being used.
Example
Take a look at the following example −
#include <stdio.h> int main(){ printf("Hello \a world\n"); return 0; }
Output
Run the code and check its output −
Hello world
Escape sequences are used widely in many other programming languages such as Java, PHP, C#, etc.