C – Basic Escape Sequences

Course Outline  | Previous LessonNext Lesson

Introduction

Escape sequences are used to further format the string and character literals. In the last lesson, the newline character, \n, which is one of the escape sequences, causes line breaks in strings. The newline character manipulates the string literal as well. Escape sequences start with a backward slash (\) as the first character followed by a certain character. Just like how the newline character is with \n. Those characters would be considered as one character as a whole. This lesson covers more, basic escape sequences.

Alert Character

The alert character, \a, makes a sound whenever a computer detects it. That character can be placed inside of a string literal. When the printf function displays a text to the screen with the alert character, it will also make a sound. The code below uses the alert character.

printf("This has a sound.\a");

The output is: This has a sound. Then the program will make a sound.

Backspace Character

The backspace character, \b, moves the computer’s cursor to the left without deleting the old character. Using the backspace character is like pressing the backspace key on the keyboard but without deleting the old character as previously said. However, when another character is typed, the new character replaces the old one. The code below uses the backspace character.

printf("Testing\bH");

The output is: TestinH.

Carriage Return

The carriage return character, \r, is somewhat similar to the backspace character. However, the difference is the cursor would start in the very beginning of the string literal. The code below utilizes that.

printf("Testing\rYes");

The output is: Yesting. The word “Yes” replaces “Tes”.

Horizontal Tab

The Horizontal Tab, \t, shifts characters to the right. It is like pressing the tab key from the keyboard. The code below uses the horizontal tab along with the newline character.

printf("Testing\n\tStatus.");

The output is:

Testing

Status.

Backslash

Some programmers may want to output a backslash within the string literal. For an example, a new programmer may try to do that successfully like the code below.

printf("Choose one: car\train");

The new programmer would expect the program to output: Choose one: car\train. However, that would not be the case. Instead, the output is: Choose one: car     rain. The computer will come across the character, \t, and it will “assume” that the programmer wants to tab rain to the right. To overcome this issue, the programmer should use a forward slash. However, if the programmer still wants to use a backslash, it is feasible. Instead of having one backslash, there needs to be two. The backslash character, \\, needs be used.

printf("Choose one: car\\train");

The output is now: Choose one: car\train. The backslash is being escaped.

Apostrophe or Single Quotation

An apostrophe or a single-quote character can be stored into memory. However, the code below is not the correct way of storing an apostrophe.

char example = ''';

This code produces an error because the second apostrophe ends the character literal. When the computer detects the third one, errors would produce. In order to fix this, the backslash can be used to produce the escaped apostrophe character \’. Thus, the code below correctly stores the apostrophe into memory.

char example = '\'';

The apostrophe or single quotation character is being escaped.

Double Quotation

Sometimes, programmers may want to display double quotes inside the string literal. Furthermore, some of them may want to do that for dialogues. The proper way of displaying double quotes is with the double-quote, escape-sequence character, \”. The code below uses that character.

printf("She said, \"I love you so much.\"");

The output is: She said, “I love you so much.” That double-quotation character is being escaped.

Table of Basic Escape Sequences

Conclusion

The escape sequences allow programmers to use additional characters that could not be done before. The alert character makes it possible to make sound. The backspace character mimics the backspace. However, it does not delete the old character until a new character is typed afterwards. The carriage return character moves the cursor to the beginning of the string literal. When the new character is typed after \r, it will replace one, some, or all of the old character(s). The horizontal tab character is the same as a user pressing the tab key from the keyboard. The backslash can be displayed by using 2 consecutive backslashes. An apostrophe or a single quotation mark can be displayed as well by using this \’ sequence. Finally, the double quotation can be displayed with this \” sequence as well.

 

Course Outline  | Previous LessonNext Lesson

Leave a Reply

Be the First to Comment!

Notify of
avatar
wpDiscuz