Escape Sequences/Execution characters

 Escape Sequence / Execution Character

Some characters such as newline, tab, backspace cannot be displayed like other normal characters. C supports the combination of backslash (\) and some characters from the C character for representing these character in our C programs. These character combinations are  known as escape sequences and are represented by two characters. The first character is "\" and second character is from the C character set. some escape sequences are given below -

Escape sequence

Meaning

purpose

\a

Beep /alert

Produce an audible and visible alert

\b

Backspace

Move the cursor to the previous position of the current line.

\f

Form feed

Moves the cursor to the initial position of the next logical page.

\r

Carriage return

Moves the cursor to the beginning of the current line

\t

Tab (horizontal)

Moves the cursor to the next horizontal tab position.

\v

Vertical tab

Moves the cursor to the next vertical tab position.

\n

New line

Move the cursor to beginning of the next line.

\\

Backslash

Presents a character with backslash (\).

\”

Double quote

Presents a character with double quote (“).

\?

Question mark

Presents a character with Question mark (?).

\ooo

Octal no.

 octal escape sequence

\xhh

Hexadecimal no.

 hexa escape sequence

\0

Null character

Used for termination of character string.

 a.) \a escape sequence :

#include<stdio.h>
int main()
{
    int x;
    printf("Input value of x:\a");
    scanf("%d",&x);
    return 0;
}
Output :
Input value of x:4

b.) \b escape sequence :

#include<stdio.h>
int main()
{
    printf("C is amazing\blaguage     ");
    // move the cursor previous position of the current line
    printf("C is amazing\blaguage\b\b\bG");
    return 0;
}
Output :
The output dependent upon compiler.
but the output of this program
C is amazinlaguage C is amazinlaguGge

c.)\n escape sequence :

#include<stdio.h>
int main()
{
    printf("C is amazing language\n");
// shift the cursor to the next line.
    printf("welcome to Educaton ki duniya!");
    return 0;
}
Output :
C is amazing language welcome to Educaton ki duniya!

d.)\r escape sequence :

#include<stdio.h>
int main()
{.
    // here are using \r.which
    // carriage return character.
    printf("C is amazing \r language\n");
    printf("welcome to Educaton ki duniya!");
    return 0;
}
output :
 Depend upon compiler
languageing welcome to Educaton ki duniya!

e.) \t escape sequence :

#include<stdio.h>
int main()
{
    printf("C is amazing \t language\t");
    printf("welcome to Educaton ki duniya!");
    return 0;
}
Output :
C is amazing language welcome to Educaton ki duniya!

f.) \v escape sequence :

#include<stdio.h>
int main()
{
    printf("Hello\v word!"); // vertical tab
    return 0;
}
Output :
Hello word!

g.) \' or \" escape sequence :

#include<stdio.h>
int main()
{
    printf("\'  Welocome to Education ki duniya\n");
    printf("\"  Welocome to Education ki duniya\n");
    return 0;
}
Output :
' Welcome to Education ki duniya " Welcome to Education ki duniya

h.) \\ escape sequence :

#include<stdio.h>
int main()
{
    printf(" Welocome to \\ EKD\n");
    return 0;
}
Output :
Welcome to \ EKD

i.) \? escape sequence :

#include<stdio.h>
int main()
{
    printf("How are you \?\?");
    return 0;
}
Output :
How are you ??

j.) \ooo escape sequence :

#include<stdio.h>
int main()
{
    // we are using \ooo escape sequence, here
    //each o in "ooo" one to three octal no.
    // digits (0,1,2,...,7)
    char *s = "\061\065";
    printf("%s",s);
    return 0;
}
Output :
15
/* here ooo is one to three octal digits
(0,1,2,3,4,5,6,7) means there must be at least
one octal digit after \ and maximum three. here
061 is the octal notation that is the ASCII value
is 1. At the place of \061 there is 1 and the output
is 15.*/

k.) \xhh escape sequence :

#include<stdio.h>
int main()
{
    // we are using \xhh escape sequence, here
    //each hh is one or more hexadecimal no.
    // digits (0,1,2,...,9,a,b...,f, A,B,....F)
    char *s = "\x4a\t\x65";
    printf("%s",s);
    return 0;
}
Output :
J e
There can be more than one hexadecimal number
after \x. here,'x4a' and '\x65' is a hexadecimal
no. and it is single char. firstly convert into
decimal notation and it is the ASCII value of char 'J'
and 'e'.
        **** Thank you ****

Comments

Popular posts from this blog

18 Amazing programs in C.