Pages

Thursday, July 14, 2016

Escape Sequences

Escape sequences are used in many programming languages (with some variations) like C, C++, Java and C#. An escape sequence is a sequence of characters that does not represent itself when used inside a character or string literal, but is translated into another character or a sequence of characters that may be difficult or impossible to represent directly.

In C, all escape sequences consist of two or more characters, the first of which is the backslash, \; the remaining characters determine the interpretation of the escape sequence. e.g \n is an escape sequence that denotes a newline character.

Suppose we want to print out Hello, on one line, and world! on the next line. One could attempt to code as follows:

#include <stdio.h>
int main() {
    printf("Hello,
            world!");
}

This is not valid in C, since a string literal may not span into multiple lines in source code. This can be worked around by printing the newline character using its numerical value (0x0a in ASCII),

#include <stdio.h>
int main() {
        printf("Hello,%cworld!", 0x0a);
}

This program prints Hello,, followed by the byte whose numerical value is 0x0a, followed by world!. While this will indeed work when the machine uses the ASCII encoding, it will not work on systems that use other encodings, that have a different numerical value for the newline character. It is also not a good solution because it still does not allow us to represent a newline character inside a literal, and instead takes advantage of the semantics of printf. In order to solve these problems and ensure maximum portability between systems, C interprets \n inside a literal as a newline character, whatever that may be on the target system:

#include <stdio.h>
int main() {
    printf("Hello,\nworld!");
}

In this code, the escape sequence \n does not stand for a backslash followed by the letter n, because the backslash causes an "escape" from the normal way characters are interpreted by the compiler. After seeing the backslash, the compiler expects another character to complete the escape sequence, and then translates the escape sequence into the bytes it is intended to represent. Thus, "Hello,\nworld!" represents a string with an embedded newline, regardless of whether it is used inside printf or anywhere else.

This raises the issue of how to represent an actual backslash inside a literal. This is done by using the escape sequence \\.

The following escape sequences are defined in standard C. This table also shows their ASCII value. However, these escape sequences can be used on any system with a C compiler, and may map to different values if the system does not use a ASCII encoding.

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

No comments:

Post a Comment