question

Pawel-9521 avatar image
0 Votes"
Pawel-9521 asked Viorel-1 rolled back

Multi-character constants with backslashes

Hi,
I'm wondering if this is intended behaviour in the Microsoft C/C++ compiler (target arch x86 or x64 - same behaviour, version 19.29.30040). I'm trying to make a multi-character constant as follows:

 #include <stdio.h>
    
 int main() {
     printf("0x%08x\n", 'ABCC');
 }

and the result is, expectedly, 0x41424343. The characters are encoded in a little endian order, so the 'C's constitute the two lowest-order bytes of the 32-bit integer.
Now I want to replace the characters 'C' with a backslash:

 #include <stdio.h>
    
 int main() {
     printf("0x%08x\n", 'AB\\\\');
 }

and the result is... 0x5c41425c. Why is that? The backslashes now occupy one lowest-order byte and one highest-order byte, which is what I would expect from the character constant '\\AB\\', but definitely not 'AB\\\\'. For comparison, gcc, clang and icc all compile the second snippet to print 0x41425c5c.

Is this intended or a bug in the compiler? I'm aware that generally multi-character constants are a portability nightmare, and according to the C++ standard they are implementation-defined. However I haven't found any information about special handling of the backslash on any Microsoft page.

If this is intended, I would suggest adding this information here https://docs.microsoft.com/en-us/cpp/cpp/string-and-character-literals-cpp to the "Microsoft-specific" section, since IMO this behaviour is undoubtedly unexpected.

c++
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Indeed seems to be the same thing

0 Votes 0 ·

If you are interested in a workaround, you can write something like "AB\\\\"_C, where _C is a user-defined literal, made by you.


0 Votes 0 ·

0 Answers