Microcontroller Embedded C Programming Lecture 18| Escape sequences in ‘C’

 

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
Figure 1. Escape sequence

 

Example:

  1. \': Single quote character
    • Represents a literal single quote character.
  2. \": Double quote character
    • Represents a literal double quote character. 
    • And if you want to print double quotes, you can use \” escape sequence.
  3. \\: Backslash character
    • Represents a literal backslash character. 
    • If you want to print Backslash, you can use \\ escape sequence.
  4. \0: Null character
    • Represents the null terminator character in C strings. 
  5. \a: Alert (bell) character
    • Produces a beep or alert sound on some systems.
  6. \b: Backspace character
    • Moves the cursor back one position without erasing.
  7. \f: Form feed character
    • Advances to the next page or form.
  8. \n: Newline character
    • Moves the cursor to the beginning of the next line.
  9. \r: Carriage return character
    • Moves the cursor to the beginning of the current line. About that, we’ll discuss in a moment.
  10. \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.
  11. \v: Vertical tab character
    • 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
Figure 2. Exercise

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!
Figure 3. Analyze the Output Bye! World!
  1. 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)
  2. 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. 
  3. The next printf will execute from here at the character H, because H is now the cursor’s current position.
  4. That’s why the old text will be overwritten with a new string,” bye ! ” 
  5. 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 !
Figure 4. Output bye ! World !

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.