Escape sequence in C
Escape sequences in C programming language:
Escape sequences in C are combinations of characters that represent special characters or actions that cannot be represented easily or visibly by regular printable characters. These sequences are used within string literals and character constants (enclosed in single quotes) to convey specific meanings.
Escape sequences start with a backslash (\
) followed by a character or combination of characters.
In the previous article, we explored one \n escape sequence, which is nothing but a new line or line feed.
Apart from that, there are various other escape sequences available that you can use in your program along with the printf function, and you can also use this with other standard library functions.
Here are some commonly used escape sequences in C:
![Figure 1. Escape sequence in C](https://fastbitlab.com/wp-content/uploads/2022/04/Figure-1-26.png)
Example:
\'
: Single quote character- Represents a literal single quote character.
\"
: Double quote character- Represents a literal double quote character.
- And if you want to print double quotes, you can use \” escape sequence.
\\
: Backslash character- Represents a literal backslash character.
- If you want to print Backslash, you can use \\ escape sequence.
\0
: Null character- Represents the null terminator character in C strings.
\a
: Alert (bell) character- Produces a beep or alert sound on some systems.
\b
: Backspace character- Moves the cursor back one position without erasing.
\f
: Form feed character- Advances to the next page or form.
\n
: Newline character- Moves the cursor to the beginning of the next line.
\r
: Carriage return character- Moves the cursor to the beginning of the current line. About that, we’ll discuss in a moment.
\t
: Horizontal Tab- Moves the cursor to the next tab stop position.
- \t is used for inserting horizontal tabs. So, a tab is nothing but a collection of four spaces. You can use \t to insert a tab.
\v
: Vertical tab character- Moves the cursor to the next vertical tab stop position.
- Moves the cursor to the next vertical tab stop position.
Difference between \n and \r
\n: moves the cursor to the beginning of the new line vertically.
\r: moves the cursor to the beginning of the current line horizontally.
To understand this, let’s do a small exercise. Here I’m going to print the text “Hello World !” and I’m going to give \r. And here give another printf statement “bye !“, as shown in Figure 2.
![Escape sequence in C](https://fastbitlab.com/wp-content/uploads/2022/04/Figure-2-20.png)
Let’s run this code and see the Output. In the Output, you can see that it is just printing “ bye ! World !”.
Now let’s analyze the Output. Remember that \r moves the cursor to the beginning of the current line horizontally.
![Figure 3. Analyze the Output Bye! World!](https://fastbitlab.com/wp-content/uploads/2022/04/Figure-3-19.png)
- In this program, when we execute the first printf statement, the Output will be “Hello World !” and the cursor will be here(!) at the last character. And this is line 1. So, “Hello World !” will be printed.(Figure 3)
- After that, there is a \r. So, when printf encounters \r, then the cursor will be moved to the beginning of the current line horizontally.
- The next printf will execute from here at the character H, because H is now the cursor’s current position.
- That’s why the old text will be overwritten with a new string,” bye ! ”
- Now ‘!’ this is the position of the new cursor. So, that’s the reason why you see the Output bye! World!.
![Figure 4. Output bye ! World !](https://fastbitlab.com/wp-content/uploads/2022/04/Figure-4-18.png)
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1