4.3 – Escape sequences or Back slash characters

by subbu on September 12, 2013

Escape sequences in C language: (Back slash characters)

These are the non-graphical characters used to perform some action on the display devices.
Every back slash character is considered as a single character, has its ASCII value.

Here in this session we are going to discourse the usage and functionality of different escape sequences with suitable examples.
Though escape sequences can be used with other functions, here at this point I am demonstrating with printf() function.

Escape Sequence ASCII Purpose
\n 10 (new line) Moves the active position to the initial position of the next line.
\t 9 (horizontal tab) Moves the active position to the next horizontal tabulation position on the current line.
\a 7 (alert) Produces an audible alert without changing the active position
\f 12 ( form feed) Moves the active position to the initial position at the start of the next logical page
\r 13 (carriage return) Moves the active position to the initial position of the current line.
\b 8 (backspace) Moves the active position to the previous position on the current line.
\v 11 (vertical tab) Moves the active position to the initial position of the next vertical tabulation position.
Printable Escape Sequences
\\ 92 To print \ at the active position
\” 34 To print ” at the active position
\’ 39 To print ‘ at the active position
\? 63 To print ? at the active position

\n

It is called a new line character used several times in the previous sessions to send the active position to the next line.

Example:

#include<stdio.h>
int main()
{
printf("Hello\nworld");
printf("\n\n");
printf("C made easy");
return 0;
}

Output:
Hello
World

C made easy


\t

It is the horizontal tab character moves the active position to the next horizontal tabulation in the current line.
Example:

#include<stdio.h>
int main()
{
printf("Hello\tWorld");
return 0;
}

Output:
Hello        World

Example:

#include<stdio.h>
int main()
{
printf("\n*\n*\t*\n*\t*\t*");
return 0;
}

Output:
*
*        *
*        *        *


\b

It is a back space character sends the active position one character back in the same line.

Example:

#include<stdio.h>
int main()
{
printf("Hello world\b\b");
return 0;
}

Output:
Hello world

Example explained:
The first \b sends the control to ‘d’, second \b sends the control to ‘l’. Now the active position is on ‘l’.

Example:

#include<stdio.h>
int main()
{
printf("Hello World\b\b\b\b\bComputer");
return 0;
}

Output:
Hello Computer

Example explained:
\b\b\b\b\b send the active position to ‘W’, The word “Computer” is overwritten on the World.


\a

It produces an audible alert without changing active position. It is generally used to get the user attention while displaying an error messages or a warnings.

Example:

#include<stdio.h>
int main()
{
int a,b,c;
printf("\aEnter the first number:");
scanf("%d",&a);
printf("\aEnter the second number:");
scanf("%d",&b);
c=a+b;
printf("\aSum of two numbers %d",c);
return 0;
}

Output:
<beep>Enter the first number:34
<beep>Enter the second number:56
<beep>Sum of two numbers 90

Example explained:
Produces <beep> sound for every input and output.

Note:
Make sure that, a speaker is provided on the mother board.


\r

Sends the active position to the initial position of the current line

Example:

#include<stdio.h>
int main()
{
printf("Hello World\rComputer");
return 0;
}

Output:
Computerrld

Example explained:
\r sends the control to the first column of the same line (‘H’ of Hello world). “Computer” is overwritten on ‘Hello Wo’. Hence the output is “Computerrld”.


\\

It prints a back slash (\) at the active position.

Example:

/* program to print codingfox\nokia as output */
#include<stdio.h>
int main()
{
printf("codingfox\nokia");
return 0;
}

Output:
codingfox
okia

Example explained:
The output is not as we expected, because ‘\n’ worked like a next line character.

Example rectified:

#include<stdio.h>
void main()
{
printf("codingfox\\nokia");
return 0;
}

Output:
codingfox\nokia


\”

It is to print “(Double quotation) at the active position.

Example:

/* program to print "codingfox" is innovative */
#include<stdio.h>
int main()
{
printf(" "codingfox" is innovative ");
return 0;
}

Output:
Error: Function call missing ) in function main

Example explained:
A printf statement must not have more than two “(Double quotes).

Example rectified:

#include<stdio.h>
int main()
{
printf(" \"codingfox\" is innovative");
return 0;
}

Output:
“codingfox” is innovative


\v:

(vertical tab) Moves the active position to the initial position of the next vertical tabulation position. It is generally used to specify next starting point to the printer. Microsoft uses it as a paragraph separator

\f

( form feed) Moves the active position to the initial position at the start of the next logical page. It can be used while writing data on to the files.

\?

It is used to print ? at the active position

\’ 

It is used to print ‘ at the active position

Guess what would be the output of following program?

#include<stdio.h>
int main()
{
printf("Hello\cWorld");
return 0;
}
Show Answer
Output in Turbo C:
HellocWorld //prints ‘c’ in place of  ‘\c’ because ‘\c’ is not an escape sequence

Output in MinGcc:
Error: ‘\c’ is not an escape sequence

Previous post:

Next post: