Monday, 25 October 2010

Escape Sequences

Escape Sequences or Character Constants

Suppose we want to print out Hello, on one line, followed by world! on the next line. One could attempt to represent the string to be printed as a single literal as follows:

#include <stdio.h>
void main() {
    printf("Hyd,
Chennai!");
}

This is not valid in C, since a string literal may not span multiple logical source lines
So solution is escape sequences

#include <stdio.h>
void main() {
    printf("Hyd \n Chennai!");
}


Escape sequence   Hex value in ASCII Character represented
\v                                0B                         Vertical Tab
\t                                09                          Horizontal Tab
\r                                0D                          Carriage Return
\nnn                               any                          The byte whose numerical value is given by nnn                                                                                       interpreted as an octal number
\n                               0A                           Newline (Line Feed); see notes below
\f                               0C                          Formfeed
\b                               08                          Backspace
\a                               07                          Alert (Beep, Bell) (added in C89)[1]
\\                               5C                          Backslash
\?                               3F                          Question mark
\'                               27                          Single quotation mark
\"                               22                          Double quotation mark
\0                                                                      Null character

It is also treated as  Constants
Sometimes, it is necessary to use newline(enter), tab, quotation mark etc. in the program which either cannot be typed or has special meaning in C programming. In such cases, escape sequence are used.

For example: \n is used for newline. The backslash( \ ) causes "escape" from the normal way the characters are interpreted by the compiler.
Escape Sequences

You may like the following posts:

No comments:

Post a Comment