Menu
×
   ❮     
HOME Computer Fundamentals C Language C++ MS-OFFICE TALLY 11/12 SCIENCE 11/12 COMM BCA BCCA BBA MCA
     ❯   

Computer

▸ Computer Fundamentals▸ What is Computer▸ History of Computer▸ Types of Computer▸ History of C Language▸ History of C Language

Computer Components

▸ Computer components▸ Input Devices▸ Output Devices▸ CPU▸ Hardware

Computer Memory

▸ Computer Memory▸ Register Memory

Computer Virus

▸ Computer virus▸ Computer virus 2

Computer Network

▸ Computer Network details

Number Systems

▸ Number Systems

C Control Statements

▸ C if else▸ C Switch Statement▸ if-else vs switch▸ C Loops▸ C Do- While Loop▸ C While Loop▸ C For Loop▸ C For Loop▸ Nested Loops in C▸ Infinite Loop in C▸ C break▸ C continue▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ C goto ▸ Type Casting

C Functions

▸ What is Function▸ Recursion in C▸ Call: Value & Reference▸ Call: Value & Reference▸ Storage Classes▸ Storage Classes

C Array

▸ 1 D - Array▸ Return an Array in C▸ Array To Function ▸ 2 - D Array▸ Return an Array in C

C Pointers

▸ C Pointers▸ C Double Pointer (Pointer to Pointer)▸ C Double Pointer (Pointer to Pointer)

C Tutorial

▸ Data Types in C▸ Keywords in C▸ C Identifiers▸ Escape Sequence in C▸ ASCII value in C▸ Constants in C▸ Static in C▸ Programming Errors in C▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Compile time vs Runtime▸ Conditional Operator in C▸ Bitwise Operator in C▸ Bitwise Operator in C▸ 2s complement in C▸ What is C Language▸ History of C Language▸ Features of C Language▸ How to install C▸ First C Program▸ Compilation process in c▸ Printf Scanft▸ Variables in C ▸ C Operators▸ Comments in C▸ C Format Specifier▸ Literals in C▸ Tokens in C▸ C Boolean▸ Programming Errors in C

Escape Sequence in C ❮ Edit Details

Programming languages like C have escape sequences as a standard feature. They enable the inclusion of special characters and control patterns in strings and character constants that are difficult to represent or write directly. In this blog post, we will explore escape sequences in C, their syntax, usage, and examples with matching outputs. Therefore, let's start this adventure to understanding escape sequences in C.

An escape sequence in the C programming language consists of a backslash () and a character that stands in for a special character or control sequence. During the compilation process, the C compiler substitutes any escape sequences it comes across with the relevant character or control sequence. It enables the use of difficult-to-represent characters like newlines, tabs, quotations, and backslashes.

It is composed of two or more characters starting with backslash \. For example: \n represents new line.

Regular Escape Sequences:

There are several escape sequence in C programming languages.

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

Now that we have a thorough understanding of each escape sequence in C,

Alarm or Beep (\a):

The alarm or beep escape sequence (a) produces an audible alert or beep sound.

Now that we have a thorough understanding of each escape sequence in C,

Alarm or Beep (\a):

The alarm or beep escape sequence (a) produces an audible alert or beep sound.

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. printf("This is an alarm sound: \a");  
  5.   
  6.     return 0;  
  7. }  

Output:

This is an alarm sound:

Backspace (\b):

The cursor can be advanced by one character with the backspace escape key (b).

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. printf("Hello\b\b\bWorld!");  
  5.   
  6.     return 0;  
  7. }  

Output:

HelloWorld!

Form Feed (\f):

The form feed escape sequence (f) is used to mimic a page break or advance to the next page.

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. printf("This is before the form feed.\fThis is after the form feed.");  
  5.   
  6.     return 0;  
  7. }  

Output:

This is before the form feed.
                            This is after the form feed.

New Line (\n):

The new line escape sequence (n) is used to insert a newline character and move the cursor to the start of the following line.

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. printf("Line 1\nLine 2");  
  5.   
  6.     return 0;  
  7. }  

Output:

Line 1
Line 2

Carriage Return (\r):

The cursor can be moved to the start of the current line by using the carriage return escape sequence (r).

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. printf("Hello\rWorld!");  
  5.   
  6.     return 0;  
  7. }  

Output:

World!

Tab (Horizontal) (\t):

The tab escape sequence (t) is used to insert a horizontal tab character and shift the cursor to the following tab stop.

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. printf("Name:\tJohn\tAge:\t25");  
  5.   
  6.     return 0;  
  7. }  

Output:

Name:   John    Age:    25

Vertical Tab (\v):

The vertical tab escape sequence (v) is used to simulate a vertical tab or shift the mouse to the following vertical tab location.

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. printf("Hello\vWorld!");  
  5.   
  6.     return 0;  
  7. }  

Output:

Hello
     World!

Backslash (\):

backslash character is inserted using the backslash escape sequence (\).

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. printf("This is a backslash: \\Hello");  
  5.   
  6.     return 0;  
  7. }  

Output:

This is a backslash: \Hello

Single Quote ('):

The single quote escape sequence (') is used to insert a single quote character.

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. printf("This is a single quote: \'Hello\'");  
  5.   
  6.     return 0;  
  7. }  

Output:

This is a single quote: 'Hello'

Double Quote ("):

double quotation character is inserted using the double quote escape sequence (").

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. printf("This is a double quote: \"Hello\"");  
  5.   
  6.     return 0;  
  7. }  

Output:

This is a double quote: "Hello"

Question Mark (?):

The question mark escape sequence (?) is used to insert a question mark character.

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. printf("This is a question mark: \?");  
  5.   
  6.     return 0;  
  7. }  

Output:

This is a question mark: ?

Octal Number (\nnn):

The character's octal value can be inserted using the octal number escape sequence (nnn).

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. printf("This is an octal value: \101");  
  5.   
  6.     return 0;  
  7. }

Output:

This is an octal value: A

Hexadecimal Number (\xhh):

character's hexadecimal value can be inserted using the hexadecimal number escape sequence (xhh).

  1. #include <stdio.h>  
  2.   
  3. int main() {  
  4. printf("This is a hexadecimal value: \x41");  
  5.   
  6.     return 0;  
  7. }  

Output:

This is a hexadecimal value: A

Null (\0):

The null character, denoted by "0", is inserted using the null escape sequence (0).

  1. #include <stdio.h>  
  2. int main() {  
  3.     char myString[] = "Hello\0World!";  
  4. printf("String: %s", myString);  
  5.   
  6.     return 0;  
  7. }  

Output:

String: Hello

Conclusion:

In conclusion, escape sequences are a crucial component of C programming that enables us to successfully handle special characters and control sequences. We may put characters into strings and character variables that are challenging to directly express using escape sequences.