Escape Sequence in C

Escape Sequence in C. Escape Sequence in C Tutorial. Learn Escape Sequence in C
Escape Sequence in C

What is Escape Sequence in C?

In C programming language, an escape sequence is a combination of characters that represents a special meaning when used within a string. It is denoted by a backslash () followed by a character or a combination of characters.

Escape sequences are used in C to represent special characters in a string that cannot be easily typed or displayed directly. For example, to include a double quote character within a string, we can use the escape sequence ” instead of typing it directly.

Types of Escape Sequences in C:

Sr. No.Escape SequenceInformation
1\aAlert or Bell
2\bBackspace
3\fForm feed
4\nNewline
5\rCarriage return
6\tHorizontal tab
7\vVertical tab
8\Backslash
9Single Quote
10Double Quote
11?Question Mark
12\0Null Character

Example:

Example code using all escape sequences:

#include <stdio.h>

int main() {
    printf("Escape Sequences in C:\n");
    
    // Use descriptive names for variables
    char horizontal_tab = '\t';
    char backslash = '\\';
    char single_quote = '\'';
    char double_quote = '\"';
    char question_mark = '\?';
    char newline = '\n';
    char carriage_return = '\r';
    char vertical_tab = '\v';
    char form_feed = '\f';
    char bell = '\a';
    char null_char = '\0';
    
    // Use printf formatting for better readability
    printf("1. Horizontal tab:   %c\n", horizontal_tab);
    printf("2. Backslash:       %c\n", backslash);
    printf("3. Single quote:    %c\n", single_quote);
    printf("4. Double quote:    %c\n", double_quote);
    printf("5. Question mark:   %c\n", question_mark);
    printf("6. Newline:         %c\n", newline);
    printf("7. Carriage return: %c\n", carriage_return);
    printf("8. Vertical tab:    %c\n", vertical_tab);
    printf("9. Form feed:       %c\n", form_feed);
    printf("10. Bell:           %c\n", bell);
    printf("11. Null character: %c\n", null_char);
    
    return 0;
}

Output:

Escape Sequences in C:
1. Horizontal tab:   	
2. Backslash:        \
3. Single quote:     '
4. Double quote:     "
5. Question mark:    ?
6. Newline:          
7. Carriage return:  
8. Vertical tab:     
9. Form feed:        
10. Bell:            
11. Null character:  

Explanation:

The above code prints various escape sequences using the printf function. We use \t for a horizontal tab, \ for a backslash, ‘ for a single quote, ” for a double quote, ? for a question mark, \n for a newline, \r for a carriage return, \v for a vertical tab, \f for a form feed, \a for a beep, and \0 for a null character.

Rules to follow while using Escape Sequences in C:

  • Escape sequences should always start with a backslash ().
  • Escape sequences are case sensitive.
  • Some escape sequences require a specific number of digits or characters to follow them, such as \x and \u.
  • Escape sequences cannot be used outside of a string or character literal.
  • Some escape sequences may have different meanings depending on the implementation.

More: