Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

number of digits in a hex escape code in C/C++

I'm having a dispute with a colleague of mine. She says that the following:

char* a = "\x000aaxz";

will/can be seen by the compiler as "\x000aa". I do not agree with her, as I think you can have a maximum number of 4 hex characters after the \x. Can you have more than 4 hex chars?

Who is right here?

like image 715
Geo Avatar asked Apr 29 '10 06:04

Geo


People also ask

What is use of '\ r escape sequence in C?

\r is a carriage return character; it tells your terminal emulator to move the cursor at the start of the line. The cursor is the position where the next characters will be rendered. So, printing a \r allows to override the current line of the terminal emulator.

What is the size of escape sequence characters?

The escape sequence is always a backslash followed by a single u and then a hex sequence of four characters. Following this convention, the variable a can be represented by the Unicode escape sequence \u0061.


2 Answers

§2.13.2/4:

The escape \xhhh consists of the backslash followed by x followed by one or more hexadecimal digits that are taken to specify the value of the desired character. There is no limit to the number of digits in a hexadecimal sequence. A sequence of octal or hexadecimal digits is terminated by the first character that is not an octal digit or a hexadecimal digit, respectively.

She is right.

However, you can terminate it early by eager catenation: the sequence of literals "\x000a" "axz" specifies a single four-character string literal. (2.13.4/3)

Also note that Unicode uses 21-bit code points; it doesn't stop at 16 bits.

like image 162
Potatoswatter Avatar answered Oct 03 '22 05:10

Potatoswatter


Quote from MSDN on C++ character constants:

Octal escape sequences, specified in the form \ooo, consist of a backslash and one, two, or three octal characters. Hexadecimal escape sequences, specified in the form \xhhh, consist of the characters \x followed by a sequence of hexadecimal digits. Unlike octal escape constants, there is no limit on the number of hexadecimal digits in an escape sequence.

from http://msdn.microsoft.com/en-us/library/6aw8xdf2.aspx

like image 20
qbeuek Avatar answered Oct 03 '22 04:10

qbeuek