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 for | Hex value in ASCII |
---|---|---|
\a | Alert(Beep Sound) | 07 |
\b | Backspace | 08 |
\e | Escape Character | 1B |
\f | Formfeed Page break | 0C |
\n | New line | 0A |
\r | Carriage return | 0D |
\t | Horizontal Tab | 09 |
\v | Vertical Tab | 0B |
\\ | Backslash | 5C |
\’ | Apostrophe | 27 |
\” | Double quotes | 22 |
\? | Question mark | 3F |
\nnn | Octal number | any |
\xhh | Hexadecimal number | any |
\uhhhh | unicode point below 10,000 hexadecimal | none |
\uhhhhhhhh | unicode point where h is a hexadecimal | none |
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 /n
#include<stdio.h> int main() { printf("Hello\n" "PrepSters"); return (0); }
Output :
Hello
PrepSters
Using /b
#include<stdio.h> int main() { printf("Hello PrepSters\b\b\b\b\bs"); return (0); }
Output :
Hello Prepsters
Using /r
#include int main() { printf("Hello \r PrepSters"); return (0); }
Output :
Hello Prepsters
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment