Escape Sequences

From RAD Studio
Jump to: navigation, search

Go Up to Character Constants Overview Index

The backslash character (\) is used to introduce an escape sequence, which allows the visual representation of certain nongraphic characters. For example, the constant \n is used to the single newline character.

A backslash is used with octal or hexadecimal numbers to represent the ASCII symbol or control code corresponding to that value; for example, '\03' for CTRL-C or '\x3F' for the question mark. You can use any string of up to three octal or any number of hexadecimal numbers in an escape sequence, provided that the value is within legal range for data type char (0 to 0xff). Larger numbers generate the compiler error Numeric constant too large. For example, the octal number \777 is larger than the maximum value allowed (\377) and will generate an error. The first nonoctal or nonhexadecimal character encountered in an octal or hexadecimal escape sequence marks the end of the sequence.

Take this example.

printf("\x0072.1 A Simple Operating System");

This is intended to be interpreted as "\x007" and "2.1 A Simple Operating System". However, the compiler treats it as the hexadecimal number "\x0072" and the literal string ".1A Simple Operating System".

To avoid such problems, rewrite your code like this:

printf("\x007" "2.1 A Simple Operating System");

Ambiguities might also arise if an octal escape sequence is followed by a nonoctal digit. For example, because 8 and 9are not legal octal digits, the constant \258 would be interpreted as a two-character constant made up of the characters \25 and 8.

The following table shows the available escape sequences.

Escape sequences

Note: You must use \\ to represent an ASCII backslash, as used in operating system paths.

Sequence

Value

Char

What it does

\a
0x07

BEL

Audible bell

\b
0x08

BS

Backspace

\f
0x0C

FF

Formfeed

\n
0x0A

LF

Newline (linefeed)

\r
0x0D

CR

Carriage return

\t
0x09

HT

Tab (horizontal)

\v
0x0B

VT

Vertical tab

\\
0x5c

\

Backslash

\'
0x27

'

Single quote (apostrophe)

\"
0x22

"

Double quote

\?
0x3F

?

Question mark

\O

any

O = a string of up to three octal digits

\xH

any

H = a string of hex digits

\XH

any

H = a string of hex digits


See Also