This C Tutorial explains Escape Sequences/Character Escapes and why they are needed.
What is an Escape Sequences in C?
One or more characters following ‘\’ (the backslash) is considered to be a single character and is called the escape sequence.
List of Escape Sequences in C:
- \? used to write multiple question marks from being interpreted as trigraphs in printf() statements.
- \” used to get the quotation marks inside the string literals.
- \’ used to write a character literal for a character.
- \\ used to write a backslash from being interpreted as a character escape.
- \a used for alert or beep.
- \b used for backspace
- \0 used for Null value.
- \n is used to insert new line.
- \t is used for tabspace.
- \nnn is commonly used to represent an octal number.
- \xhh is used to represent an hexadecimal number.
Escape sequences are used in a program to override the special meaning of characters in particular contexts!
For example:
Example 1:
advertisement
printf("What is your name? Are you \"some Xyz\"?"); /* * OUTPUT: What is your name? Are you "some XYZ"? * Note the Name in output enclosed in Quotation Marks */
Example 2:
#include<stdio.h> int main() { printf("Learn more about \t escape sequences\n"); printf("@ Sanfoundry"); return (0); }
Output:
Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
Learn more about escape sequences @ Sanfoundry
Trigraphs
Trigraphs are all three-lettered characters, all begin with ?? (double question marks) followed by a character, represent some other character. Basically, they are used to complement a Character Set lacking for some characters. For example:
??( represent [ ??) represent ]
Sanfoundry Global Education & Learning Series – 1000 C Tutorials.
advertisement
If you wish to look at all C Tutorials, go to C Tutorials.
Related Posts:
- Apply for Computer Science Internship
- Watch Advanced C Programming Videos
- Check C Books
- Apply for C Internship
- Practice BCA MCQs