I encountered a strange issue while working on a C++ program with VisusalStuidio 2021. When I used cout << ‘/n’;, I expected it to simply print /n or values in ASCII(47110), but instead, it printed 12142. This behavior is confusing and I couldn’t find a clear explanation for it.
I’ve tried to go googling but the only answers are very sketchy, so I’d appreciate a detailed answer, thanks!Hope someone can help me。
2
Answers
you are seeing multicharacter constant, look in character_constant and search for
multicharacter constant
.you are just seeing the concatenation of the bytes of both
/
andn
converted to an int, you can print them in hex to see their hex representation is equivalent.Just use the double quotes:
For anyone wondering why the value is 12142, the likely answer is that in the OP’s implementation, ‘/’ has the integral value 47, ‘n’ has the integral value 110, and these are "concatenated" into an int a la <47><110> (leaving endianness out of the picture), giving a value of 47*2^8 + 110 = 12142