Английская Википедия:Escape sequences in C

Материал из Онлайн справочника
Перейти к навигацииПерейти к поиску

Шаблон:Short description Шаблон:More footnotes Шаблон:Use American English Шаблон:Use dmy dates Escape sequences are used in the programming languages C and C++, and their design was copied in many other languages such as Java, PHP, C#, etc. 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, Шаблон:Mono (called the "Escape character"); the remaining characters determine the interpretation of the escape sequence. For example, Шаблон:Mono is an escape sequence that denotes a newline character.

Motivation

Suppose we want to print out Шаблон:Mono on one line, followed by Шаблон:Mono on the next line. One could attempt to represent the string to be printed as a single literal as follows:

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

return 0;
}

This is not valid in C, since a string literal may not span multiple logical source lines. This can be worked around by printing the newline character using its numerical value (Шаблон:Mono in ASCII),

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

This instructs the program to print Шаблон:Mono, followed by the byte whose numerical value is Шаблон:Mono, followed by Шаблон:Mono. 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 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 Шаблон:Mono inside a literal as a newline character, whatever that may be on the target system:

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

In this code, the escape sequence Шаблон:Mono does not stand for a backslash followed by the letter Шаблон:Mono, 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, Шаблон:Mono represents a string with an embedded newline, regardless of whether it is used inside Шаблон:Mono 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 Шаблон:Mono, as seen in the next section.

Some languages don't have escape sequences, for example Pascal. Instead a command including a newline would be used (Шаблон:Mono includes a newline, Шаблон:Mono excludes it).

writeln('Hello');
write('world!');

Table of escape sequences

The following escape sequences are defined in standard C. This table also shows the values they map to in ASCII. 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 character encoding based on ASCII.

Escape sequence Hex value in ASCII Character represented
Шаблон:Mono 07 Alert (Beep, Bell) (added in C89)[1]
Шаблон:Mono 08 Backspace
Шаблон:MonoШаблон:Ref 1B Escape character
Шаблон:Mono 0C Formfeed Page Break
Шаблон:Mono 0A Newline (Line Feed); see notes below
Шаблон:Mono 0D Carriage Return
Шаблон:Mono 09 Horizontal Tab
Шаблон:Mono 0B Vertical Tab
Шаблон:Mono 5C Backslash
Шаблон:Mono 27 Apostrophe or single quotation mark
Шаблон:Mono 22 Double quotation mark
Шаблон:Mono 3F Question mark (used to avoid trigraphs)
Шаблон:MonoШаблон:Ref any The byte whose numerical value is given by nnn interpreted as an octal number
Шаблон:Mono any The byte whose numerical value is given by hh… interpreted as a hexadecimal number
Шаблон:MonoШаблон:Ref none Unicode code point below 10000 hexadecimal (added in C99)Шаблон:R
Шаблон:MonoШаблон:Ref none Unicode code point where h is a hexadecimal digit
Note 1.Шаблон:NoteCommon non-standard code; see the Notes section below.
Note 2.Шаблон:NoteThere may be one, two, or three octal numerals n present; see the Notes section below.
Note 3.Шаблон:Note\u takes 4 hexadecimal digits h; see the Notes section below.
Note 4.Шаблон:Note\U takes 8 hexadecimal digits h; see the Notes section below.

Notes

Шаблон:Mono produces one byte, despite the fact that the platform may use more than one byte to denote a newline, such as the DOS/Windows CRLF sequence, Шаблон:Mono. The translation from Шаблон:Mono to Шаблон:Mono on DOS and Windows occurs when the byte is written out to a file or to the console, and the inverse translation is done when text files are read.

A hex escape sequence must have at least one hex digit following Шаблон:Mono, with no upper bound; it continues for as many hex digits as there are. Thus, for example, Шаблон:Mono denotes the byte with the numerical value ABCDEF16, followed by the letter Шаблон:Mono, which is not a hex digit. However, if the resulting integer value is too large to fit in a single byte, the actual numerical value assigned is implementation-defined. Most platforms have 8-bit Шаблон:Mono types, which limits a useful hex escape sequence to two hex digits. However, hex escape sequences longer than two hex digits might be useful inside a wide character or wide string literal(prefixed with L):

char s1[] = "\x12";       // single char with value 0x12 (18 in decimal)
char s1[] = "\x1234";     // single char with implementation-defined value, unless char is long enough
wchar_t s2[] = L"\x1234"; // single wchar_t with value 0x1234, provided wchar_t is long enough (16 bits suffices)

An octal escape sequence consists of Шаблон:Mono followed by one, two, or three octal digits. The octal escape sequence ends when it either contains three octal digits already, or the next character is not an octal digit. For example, Шаблон:Mono is a single octal escape sequence denoting a byte with numerical value 9 (11 in octal), rather than the escape sequence Шаблон:Mono followed by the digit Шаблон:Mono. However, Шаблон:Mono is the octal escape sequence Шаблон:Mono followed by the digit Шаблон:Mono. In order to denote the byte with numerical value 1, followed by the digit Шаблон:Mono, one could use Шаблон:Mono, since C automatically concatenates adjacent string literals. Note that some three-digit octal escape sequences may be too large to fit in a single byte; this results in an implementation-defined value for the byte actually produced. The escape sequence Шаблон:Mono is a commonly used octal escape sequence, which denotes the null character, with value zero.

Non-standard escape sequences

A sequence such as Шаблон:Mono is not a valid escape sequence according to the C standard as it is not found in the table above. The C standard requires such "invalid" escape sequences to be diagnosed (i.e., the compiler must print an error message). Notwithstanding this fact, some compilers may define additional escape sequences, with implementation-defined semantics. An example is the Шаблон:Mono escape sequence, which has 1B as the hexadecimal value in ASCII, represents the escape character, and is supported in GCC,[2] clang and tcc. It wasn't however added to the C standard repertoire, because it has no meaningful equivalent in some character sets (such as EBCDIC).[1]

Universal character names

From the C99 standard, C has also supported escape sequences that denote Unicode code points in string literals. Such escape sequences are called universal character names, and have the form Шаблон:Mono or Шаблон:Mono, where Шаблон:Mono stands for a hex digit. Unlike the other escape sequences considered, a universal character name may expand into more than one code unit.

The sequence Шаблон:Mono denotes the code point Шаблон:Mono, interpreted as a hexadecimal number. The sequence Шаблон:Mono denotes the code point Шаблон:Mono, interpreted as a hexadecimal number. (Therefore, code points located at U+10000 or higher must be denoted with the Шаблон:Mono syntax, whereas lower code points may use Шаблон:Mono or Шаблон:Mono.) The code point is converted into a sequence of code units in the encoding of the destination type on the target system. For example (where the encoding is UTF-8, and UTF-16 for Шаблон:Mono):

char s1[] = "\xC0"; // A single byte with the value 0xC0, not valid UTF-8
char s2[] = "\u00C0"; // Two bytes with values 0xC3, 0x80, the UTF-8 encoding of U+00C0
wchar_t s3[] = L"\xC0"; // A single wchar_t with the value 0x00C0
wchar_t s4[] = L"\u00C0"; // A single wchar_t with the value 0x00C0

A value greater than Шаблон:Mono may be represented by a single Шаблон:Mono if the UTF-32 encoding is used, or two if UTF-16 is used.

Importantly, the universal character name Шаблон:Mono always denotes the character "À", regardless of what kind of string literal it is used in, or the encoding in use. The octal and hex escape sequences always denote certain sequences of numerical values, regardless of encoding. Therefore, universal character names are complementary to octal and hex escape sequences; while octal and hex escape sequences represent code units, universal character names represent code points, which may be thought of as "logical" characters.

See also

References

Шаблон:Reflist

Further reading

  1. 1,0 1,1 Ошибка цитирования Неверный тег <ref>; для сносок Rationale_2003_C не указан текст
  2. Ошибка цитирования Неверный тег <ref>; для сносок GCC не указан текст