Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does \x mean in C/C++?

Tags:

c

syntax

Example:

char arr[] = "\xeb\x2a"; 

BTW, are the following the same?

"\xeb\x2a" vs. '\xeb\x2a'

like image 634
user198729 Avatar asked Mar 30 '10 17:03

user198729


2 Answers

\x indicates a hexadecimal character escape. It's used to specify characters that aren't typeable (like a null '\x00').

And "\xeb\x2a" is a literal string (type is char *, 3 bytes, null-terminated), and '\xeb\x2a' is a character constant (type is int, 2 bytes, not null-terminated, and is just another way to write 0xEB2A or 60202 or 0165452). Not the same :)

like image 129
Seth Avatar answered Sep 27 '22 01:09

Seth


As other have said, the \x is an escape sequence that starts a "hexadecimal-escape-sequence".

Some further details from the C99 standard:

When used inside a set of single-quotes (') the characters are part of an "integer character constant" which is (6.4.4.4/2 "Character constants"):

a sequence of one or more multibyte characters enclosed in single-quotes, as in 'x'.

and

An integer character constant has type int. The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer. The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined.

So the sequence in your example of '\xeb\x2a' is an implementation defined value. It's likely to be the int value 0xeb2a or 0x2aeb depending on whether the target platform is big-endian or little-endian, but you'd have to look at your compiler's documentation to know for certain.

When used inside a set of double-quotes (") the characters specified by the hex-escape-sequence are part of a null-terminated string literal.

From the C99 standard 6.4.5/3 "String literals":

The same considerations apply to each element of the sequence in a character string literal or a wide string literal as if it were in an integer character constant or a wide character constant, except that the single-quote ' is representable either by itself or by the escape sequence \', but the double-quote " shall be represented by the escape sequence \".


Additional info:

In my opinion, you should avoid avoid using 'multi-character' constants. There are only a few situations where they provide any value over using an regular, old int constant. For example, '\xeb\x2a' could be more portably be specified as 0xeb2a or 0x2aeb depending on what value you really wanted.

One area that I've found multi-character constants to be of some use is to come up with clever enum values that can be recognized in a debugger or memory dump:

enum CommandId {     CMD_ID_READ  = 'read',     CMD_ID_WRITE = 'writ',     CMD_ID_DEL   = 'del ',     CMD_ID_FOO   = 'foo ' }; 

There are few portability problems with the above (other than platforms that have small ints or warnings that might be spewed). Whether the characters end up in the enum values in little- or big-endian form, the code will still work (unless you're doing some else unholy with the enum values). If the characters end up in the value using an endianness that wasn't what you expected, it might make the values less easy to read in a debugger, but the 'correctness' isn't affected.

like image 29
Michael Burr Avatar answered Sep 27 '22 01:09

Michael Burr