Escape Sequences in C

Escape Sequences in C

What are Escape Characters in C ?

Like every other programming languages, C programming language also has a predefined set of characters, which a programmer can use while coding in C. There are a total of 256 characters in C Language, which are further divided into two categories

  • ASCII Characters
  • Extended ASCII Characters

Other than these 256 characters, there are some other special characters also present in C, which do not represent themselves, they are categorized as Escape Characters or Escape Sequences in C Programming Language

Difference between ASCII Characters and Escape Sequences in C

Escape Sequences in C are use to represent other characters, which generally are not present in ASCII Characters, and are hard or impossible to represent without using Escape Characters. Escape Characters are usually followed by a ‘backslash‘ and one or more ‘character‘, for example-: \n, \t, etc.  As we have discussed above, Escape Characters do not represent themselves, but they are used to represent some other characters or operations. Below is a table which describes about all the Escape Sequences and their use.
Note-: Not all the characters followed by ‘backslash(\)’ will be a Escape Character i.e; if we write

  • char str = ‘\c’;

Then, it will not be considered as a Escape Character, and the compiler will throw  a error.

List of All the Escape Sequences in C

Escape Characters          Use forHex value in ASCII
          \aAlert(Beep Sound)07
          \bBackspace08
          \eEscape Character1B
          \fFormfeed Page break0C
          \nNew line0A
          \rCarriage return0D
          \tHorizontal Tab09
          \vVertical Tab0B
         \\Backslash5C
          \’Apostrophe27
         \”Double quotes22
         \?Question mark3F
        \nnnOctal numberany
        \xhhHexadecimal numberany
       \uhhhhunicode point below 10,000 hexadecimal none
   \uhhhhhhhhunicode point where h is a hexadecimalnone

C Code for using Escape Sequences

Using  /a
#include <stdio.h> 
int main() 
{ 
    printf("Hello P\ar\ae\ap\aS\at\ae\ar\a"); 
    return (0); 
}
Output
Hello PrepSter

Using  /b

#include  <stdio.h>
int main() 
{ 
    printf("Hello PrepSters\b\b\b\b\bs"); 
    return (0); 
}
Output
Hello Prepster
Using  /n
#include  <stdio.h>
int main() 
{ 
    printf("Hello\n" "PrepSters"); 
    return (0); 
}
Output
Hello
PrepSters

Using  /r

#include  <stdio.h>
int main() 
{ 
    printf("Hello \r PrepSters"); 
    return (0); 
}
Output
Prepsters