Escape Sequence in C
For Complete YouTube Video: Click Here
In this class, we will try to understand Escape Sequence in C.
In our previous class on Formatted I/O, we have modified the printf() function of the first program.
The modification is as shown in the image below.
In the first printf() function of the program, we used \n at the end.
The \n is an escape sequence.
The escape sequences can be used in the format string of the printf() function to do a specific action.
The image below shows some of the escape sequences.
\n is used to move the cursor to a new line.
For example, consider the following format string “Welcome to “Learning Monkey” Start learning. ” to be used in the printf() function as shown below.
printf(“Welcome to “Learning Monkey” Start learning. “)
Here the printf() function will generate an error because the double quotes used before Learning will be considered the end of the format string.
C can resolve this by using \” escape sequence.
Let us rewrite the printf() by using \” escape sequence.
printf(“Welcome to \”Learning Monkey\” Start learning. “)
The double quotes are considered the characters, not as the end of the format string.
Similarly, consider another printf() function given below.
printf(“Welcome to \Learning Monkey Start learning. “)
In the above printf, we want to print \ character before L, but the compiler will consider \L as an escape sequence.
As C doesn’t support \L, it will generate a warning.
To consider \ as a character, we use \ escape sequence.
Let us rewrite the printf() by using \ escape sequence.
printf(“Welcome to \Learning Monkey Start learning. “)