exebook
exebook

Reputation: 33950

Ending hex-specified section of a C string literal

char *a = "A\x01B";

I typed this and I meant A + \x01 + B, but the compiler thought that I meant A + \x1B. I was thinking it parses two characters after the \x as a hexadecimal value, but appears it is not. Then I thought maybe it parses three of them and typed:

char *a = "A\x001B";

But the result was the same, in fact even

char *a = "A\x000000000001B";

still means A+ 0x1b

So how do I get my next character, which is B parsed into a string literal as a character after the \x1?

Upvotes: 3

Views: 75

Answers (1)

KamilCuk
KamilCuk

Reputation: 141613

how do I get my next character, which is B parsed into a string literal as a character after the \x1?

You can do:

const char *a = "A\x01" "B";
const char *a = "A\x1""B";
const char *a = "A\001B";
const char *a = "A\01B";
const char *a = "A\1B";

With slightly different meaning:

const char *a = (const char[]){'A', 1, 'B', 0};
const char a[] = {'A', 1, 'B', 0};

Upvotes: 6

Related Questions