8

I know that hex-decimal number is usually prefixed with 0x in C/C++ language. For example, 0x5A means 90 in decimal. But I saw an example code using single-quoted character with '\x'.

BYTE outputBuffer[index++] = '\x5A'; // instead of 0x5A

Is the meaning of '\x5A' exactly the same as 0x5A?

If so, why is there alternative way of hex-decimal notation?

yufit_in_Japan
  • 583
  • 11
  • 23

1 Answers1

11

For a character, both are quite equal.

But only one can be mixed into a string with other normal characters. "ABC\x5A"

And only one can be used to initialize a large integral type: long long x = 0x1234567812345678LL;

Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • 9
    In C++, '\x5A' is of type `char`, while 0x5A is of type `int`. They are not equal. – Siyuan Ren Feb 16 '14 at 05:44
  • @C.R.: It's true that they have only value equality, not type equality. But in the context of the question, being assigned to a variable of fixed type, we care about only the value and any value adjustments which take place as a result of widening or narrowing conversions. – Ben Voigt Feb 16 '14 at 15:21
  • @BenVoigt Sorry, it's indeed wrong, I've deleted my incorrect comment. Thanks for pointing it out. – Searene Sep 10 '18 at 13:33