escape-sequence

“When characters act in ways the reader wasn’t expecting, the reader’s curiosity is immediately piqued.” K.M. Weiland

There are some types of characters that we can’t write them down on a paper but they do exist! We call these types of character as Nonprinting Characters.

For example alarm sound (or simply beep) in computers.

Another example is “Newline”! Believe or not, Newline is a character and in just a few seconds you’ll see its representation in C language.

Another example is Backspace! Have you ever thought when you hit that Backspace key on your keyboard what type of character are you sending to your computer?! It is backspace character and it’ll replace any character before itself with empty-space.

Also there are some types of character that we can’t just simply put them in a variable of type char.

For example the single quote character (‘) is one of those types of characters that you can’t just put it in a variable of type char

//WRONG
char character = ''';

This is because we use two single-quote and in between, the character that we want to assign to a variable. So using the third single-quote makes the C compiler confused and as a result it’ll stop compiling the source code and it’ll return error.

In order to solve these problems, C provided a set of special symbol sequences also known as escape sequences that we can use instead of those characters mentioned above and some others that you’ll see in a moment.

The effect of using these escape-sequences is exactly the same as if we’re using those especial characters.

Sequence Meaning
a Alert (ANSI C)
b Backspace
f Form feed
n Newline
r Carriage return
t Horizontal tab
v Vertical tab
\ Backslash ()
Single quote (‘)
Double quote (“)
? Question mark (?)
oo Octal value. (o represents an octal digit)
xhh Hexadecimal value. (h represents a hexadecimal digit.)

Example:

char character = ''';

Now the character variable is holding the single quote (‘) value and the compiler will not complain about it.

Another example:

Note: in the example below we’re using printf function, if you don’t know what function is or what this function does, please check the function and printf function sections.

#include <stdio.h>

int
main() {

printf("You'll here 3 beeps now: a a a nThis message is printing on a new line. ");
return 0;
}

In the example above we’ve used beep alarm via a 3 times so you’ll hear 3 quick beep when you run the example. Also because we used Newline character n, the next message after this character will be printed on a new line.