Escape Sequence In Dev C%2b%2b

-->

  1. Escape Sequence In Dev C 2b 2b 3
  2. Escape Sequence In Dev C 2b 2b 1
  3. Escape Sequence In Dev C 2b 2b 4
  4. Escape Sequence In Dev C 2b 2b 1b

Escape Sequence In Dev C 2b 2b 3

C++ supports various string and character types, and provides ways to express literal values of each of these types. In your source code, you express the content of your character and string literals using a character set. Universal character names and escape characters allow you to express any string using only the basic source character set. A raw string literal enables you to avoid using escape characters, and can be used to express all types of string literals. You can also create std::string literals without having to perform extra construction or conversion steps.

String literals can have no prefix, or u8, L, u, and U prefixes to denote narrow character (single-byte or multi-byte), UTF-8, wide character (UCS-2 or UTF-16), UTF-16 and UTF-32 encodings, respectively. A raw string literal can have R, u8R, LR, uR, and UR prefixes for the raw version equivalents of these encodings. To create temporary or static std::string values, you can use string literals or raw string literals with an s suffix. For more information, see the String literals section below. For more information on the basic source character set, universal character names, and using characters from extended codepages in your source code, see Character sets.

Character literals

A character literal is composed of a constant character. It's represented by the character surrounded by single quotation marks. There are five kinds of character literals:

Commonly used escape sequences are, t and a. The escape sequences are enclosed in single quotes. Is a new line character and helps in transferring control to the next line. What is char data type in C? Character data type in C, enables us to store character type values in it. By, character type values, we mean, all the values enclosed into single quotes such as ‘x’, ‘1’, ‘@’, etc. The following table describes simple escape sequences with a single action command directly after the ESC character. These sequences have no parameters and take effect immediately. All commands in this table are generally equivalent to calling the SetConsoleCursorPosition console API to place the cursor. Generally, an escape sequence begins with a backslash ‘ ’ followed by a character or characters. The c compiler interprets any character followed by a ‘ ’ as an escape sequence. Escape sequences are used to format the output text and are not generally displayed on the screen. Each escape sequence has its own predefined function.

  • Ordinary character literals of type char, for example 'a'

  • UTF-8 character literals of type char (char8_t in C++20), for example u8'a'

  • Wide-character literals of type wchar_t, for example L'a'

  • UTF-16 character literals of type char16_t, for example u'a'

  • UTF-32 character literals of type char32_t, for example U'a'

The character used for a character literal may be any character, except for the reserved characters backslash (), single quotation mark ('), or newline. Reserved characters can be specified by using an escape sequence. Characters may be specified by using universal character names, as long as the type is large enough to hold the character.

Encoding

Character literals are encoded differently based their prefix.

  • A character literal without a prefix is an ordinary character literal. The value of an ordinary character literal containing a single character, escape sequence, or universal character name that can be represented in the execution character set has a value equal to the numerical value of its encoding in the execution character set. An ordinary character literal that contains more than one character, escape sequence, or universal character name is a multicharacter literal. A multicharacter literal or an ordinary character literal that can't be represented in the execution character set has type int, and its value is implementation-defined. For MSVC, see the Microsoft-specific section below.

  • A character literal that begins with the L prefix is a wide-character literal. The value of a wide-character literal containing a single character, escape sequence, or universal character name has a value equal to the numerical value of its encoding in the execution wide-character set unless the character literal has no representation in the execution wide-character set, in which case the value is implementation-defined. The value of a wide-character literal containing multiple characters, escape sequences, or universal character names is implementation-defined. For MSVC, see the Microsoft-specific section below.

  • A character literal that begins with the u8 prefix is a UTF-8 character literal. The value of a UTF-8 character literal containing a single character, escape sequence, or universal character name has a value equal to its ISO 10646 code point value if it can be represented by a single UTF-8 code unit (corresponding to the C0 Controls and Basic Latin Unicode block). If the value can't be represented by a single UTF-8 code unit, the program is ill-formed. A UTF-8 character literal containing more than one character, escape sequence, or universal character name is ill-formed.

  • A character literal that begins with the u prefix is a UTF-16 character literal. The value of a UTF-16 character literal containing a single character, escape sequence, or universal character name has a value equal to its ISO 10646 code point value if it can be represented by a single UTF-16 code unit (corresponding to the basic multi-lingual plane). If the value can't be represented by a single UTF-16 code unit, the program is ill-formed. A UTF-16 character literal containing more than one character, escape sequence, or universal character name is ill-formed.

  • A character literal that begins with the U prefix is a UTF-32 character literal. The value of a UTF-32 character literal containing a single character, escape sequence, or universal character name has a value equal to its ISO 10646 code point value. A UTF-32 character literal containing more than one character, escape sequence, or universal character name is ill-formed.

Escape sequences

There are three kinds of escape sequences: simple, octal, and hexadecimal. Escape sequences may be any of the following values:

ValueEscape sequence
newlinen
backslash
horizontal tabt
question mark? or ?
vertical tabv
single quote'
backspaceb
double quote'
carriage returnr
the null character0
form feedf
octalooo
alert (bell)a
hexadecimalxhhh

An octal escape sequence is a backslash followed by a sequence of one to three octal digits. An octal escape sequence terminates at the first character that's not an octal digit, if encountered sooner than the third digit. The highest possible octal value is 377.

A hexadecimal escape sequence is a backslash followed by the character x, followed by a sequence of one or more hexadecimal digits. Leading zeroes are ignored. In an ordinary or u8-prefixed character literal, the highest hexadecimal value is 0xFF. In an L-prefixed or u-prefixed wide character literal, the highest hexadecimal value is 0xFFFF. In a U-prefixed wide character literal, the highest hexadecimal value is 0xFFFFFFFF.

This sample code shows some examples of escaped characters using ordinary character literals. The same escape sequence syntax is valid for the other character literal types.

The backslash character () is a line-continuation character when it's placed at the end of a line. If you want a backslash character to appear as a character literal, you must type two backslashes in a row (). For more information about the line continuation character, see Phases of Translation.

Microsoft-specific

To create a value from a narrow multicharacter literal, the compiler converts the character or character sequence between single quotes into 8-bit values within a 32-bit integer. Multiple characters in the literal fill corresponding bytes as needed from high-order to low-order. The compiler then converts the integer to the destination type following the usual rules. For example, to create a char value, the compiler takes the low-order byte. To create a wchar_t or char16_t value, the compiler takes the low-order word. The compiler warns that the result is truncated if any bits are set above the assigned byte or word.

An octal escape sequence that appears to contain more than three digits is treated as a 3-digit octal sequence, followed by the subsequent digits as characters in a multicharacter literal, which can give surprising results. For example:

Escape sequences that appear to contain non-octal characters are evaluated as an octal sequence up to the last octal character, followed by the remaining characters as the subsequent characters in a multicharacter literal. Warning C4125 is generated if the first non-octal character is a decimal digit. For example:

An octal escape sequence that has a higher value than 377 causes error C2022: 'value-in-decimal': too big for character.

Dev

An escape sequence that appears to have hexadecimal and non-hexadecimal characters is evaluated as a multicharacter literal that contains a hexadecimal escape sequence up to the last hexadecimal character, followed by the non-hexadecimal characters. A hexadecimal escape sequence that contains no hexadecimal digits causes compiler error C2153: 'hex literals must have at least one hex digit'.

If a wide character literal prefixed with L contains a multicharacter sequence, the value is taken from the first character, and the compiler raises warning C4066. Subsequent characters are ignored, unlike the behavior of the equivalent ordinary multicharacter literal.

The Microsoft-specific section ends here.

Universal character names

In character literals and native (non-raw) string literals, any character may be represented by a universal character name. Universal character names are formed by a prefix U followed by an eight-digit Unicode code point, or by a prefix u followed by a four-digit Unicode code point. All eight or four digits, respectively, must be present to make a well-formed universal character name.

Surrogate Pairs

Universal character names can't encode values in the surrogate code point range D800-DFFF. For Unicode surrogate pairs, specify the universal character name by using UNNNNNNNN, where NNNNNNNN is the eight-digit code point for the character. The compiler generates a surrogate pair if necessary.

In C++03, the language only allowed a subset of characters to be represented by their universal character names, and allowed some universal character names that didn’t actually represent any valid Unicode characters. This mistake was fixed in the C++11 standard. In C++11, both character and string literals and identifiers can use universal character names. For more information on universal character names, see Character Sets. For more information about Unicode, see Unicode. For more information about surrogate pairs, see Surrogate Pairs and Supplementary Characters.

String literals

A string literal represents a sequence of characters that together form a null-terminated string. The characters must be enclosed between double quotation marks. There are the following kinds of string literals:

Narrow string literals

A narrow string literal is a non-prefixed, double-quote delimited, null-terminated array of type const char[n], where n is the length of the array in bytes. A narrow string literal may contain any graphic character except the double quotation mark ('), backslash (), or newline character. A narrow string literal may also contain the escape sequences listed above, and universal character names that fit in a byte.

UTF-8 encoded strings

A UTF-8 encoded string is a u8-prefixed, double-quote delimited, null-terminated array of type const char[n], where n is the length of the encoded array in bytes. A u8-prefixed string literal may contain any graphic character except the double quotation mark ('), backslash (), or newline character. A u8-prefixed string literal may also contain the escape sequences listed above, and any universal character name.

Wide string literals

A wide string literal is a null-terminated array of constant wchar_t that is prefixed by 'L' and contains any graphic character except the double quotation mark ('), backslash (), or newline character. A wide string literal may contain the escape sequences listed above and any universal character name.

char16_t and char32_t (C++11)

C++11 introduces the portable char16_t (16-bit Unicode) and char32_t (32-bit Unicode) character types:

Raw string literals (C++11)

A raw string literal is a null-terminated array—of any character type—that contains any graphic character, including the double quotation mark ('), backslash (), or newline character. Raw string literals are often used in regular expressions that use character classes, and in HTML strings and XML strings. For examples, see the following article: Bjarne Stroustrup's FAQ on C++11.

A delimiter is a user-defined sequence of up to 16 characters that immediately precedes the opening parenthesis of a raw string literal, and immediately follows its closing parenthesis. For example, in R'abc(Hello'()abc' the delimiter sequence is abc and the string content is Hello'(. You can use a delimiter to disambiguate raw strings that contain both double quotation marks and parentheses. This string literal causes a compiler error:

But a delimiter resolves it:

You can construct a raw string literal that contains a newline (not the escaped character) in the source:

std::string literals (C++14)

std::string literals are Standard Library implementations of user-defined literals (see below) that are represented as 'xyz's (with a s suffix). This kind of string literal produces a temporary object of type std::string, std::wstring, std::u32string, or std::u16string, depending on the prefix that is specified. When no prefix is used, as above, a std::string is produced. L'xyz's produces a std::wstring. u'xyz's produces a std::u16string, and U'xyz's produces a std::u32string.

The s suffix may also be used on raw string literals:

std::string literals are defined in the namespace std::literals::string_literals in the <string> header file. Because std::literals::string_literals, and std::literals are both declared as inline namespaces, std::literals::string_literals is automatically treated as if it belonged directly in namespace std.

Size of string literals

For ANSI char* strings and other single-byte encodings (but not UTF-8), the size (in bytes) of a string literal is the number of characters plus 1 for the terminating null character. For all other string types, the size isn't strictly related to the number of characters. UTF-8 uses up to four char elements to encode some code units, and char16_t or wchar_t encoded as UTF-16 may use two elements (for a total of four bytes) to encode a single code unit. This example shows the size of a wide string literal in bytes:

Notice that strlen() and wcslen() don't include the size of the terminating null character, whose size is equal to the element size of the string type: one byte on a char* or char8_t* string, two bytes on wchar_t* or char16_t* strings, and four bytes on char32_t* strings.

Escape Sequence In Dev C 2b 2b 1

The maximum length of a string literal is 65,535 bytes. This limit applies to both narrow string literals and wide string literals.

Modifying string literals

Because string literals (not including std::string literals) are constants, trying to modify them—for example, str[2] = 'A'—causes a compiler error.

Microsoft-specific

In Microsoft C++, you can use a string literal to initialize a pointer to non-const char or wchar_t. This non-const initialization is allowed in C99 code, but is deprecated in C++98 and removed in C++11. An attempt to modify the string causes an access violation, as in this example:

You can cause the compiler to emit an error when a string literal is converted to a non-const character pointer when you set the /Zc:strictStrings (Disable string literal type conversion) compiler option. We recommend it for standards-compliant portable code. It's also a good practice to use the auto keyword to declare string literal-initialized pointers, because it resolves to the correct (const) type. For example, this code example catches an attempt to write to a string literal at compile time:

In some cases, identical string literals may be pooled to save space in the executable file. In string-literal pooling, the compiler causes all references to a particular string literal to point to the same location in memory, instead of having each reference point to a separate instance of the string literal. To enable string pooling, use the /GF compiler option.

Escape Sequence In Dev C 2b 2b 4

The Microsoft-specific section ends here.

Concatenating adjacent string literals

Adjacent wide or narrow string literals are concatenated. This declaration:

is identical to this declaration:

Escape Sequence In Dev C 2b 2b 1b

and to this declaration:

Using embedded hexadecimal escape codes to specify string literals can cause unexpected results. The following example seeks to create a string literal that contains the ASCII 5 character, followed by the characters f, i, v, and e:

The actual result is a hexadecimal 5F, which is the ASCII code for an underscore, followed by the characters i, v, and e. To get the correct result, you can use one of these escape sequences:

std::string literals, because they're std::string types, can be concatenated with the + operator that is defined for basic_string types. They can also be concatenated in the same way as adjacent string literals. In both cases, the string encoding and the suffix must match:

String literals with universal character names

Native (non-raw) string literals may use universal character names to represent any character, as long as the universal character name can be encoded as one or more characters in the string type. For example, a universal character name representing an extended character can't be encoded in a narrow string using the ANSI code page, but it can be encoded in narrow strings in some multi-byte code pages, or in UTF-8 strings, or in a wide string. In C++11, Unicode support is extended by the char16_t* and char32_t* string types:

See also

Character sets
Numeric, Boolean, and pointer literals
User-Defined Literals