1

MSDN https://msdn.microsoft.com/en-us/library/69ze775t.aspx described VS character literal as follows:

Multiple characters in the literal fill corresponding bytes as needed from high->order to low-order. To create a char value, the compiler takes the low-order byte.

According to the document, the following result should be what I want.

unsigned int i = '1234'; // i = 0x34333231, memory[low->high] [0x31, 0x32, 0x33, 0x34]

However, when escape sequences appears, things changed, here's the result I did on my pc.

unsigned int i = '1234'; // i = 0x34333231
unsigned int j = '\1\2\3\4\'; // j = 0x01020304 <- ???????

Wait a minute, what just happened here? I'm looking forward to that variant j is 0x04030201. Where's the high-order to low-order thing? I can't figure it out by myself.

This should be my first question. Why the compiler doesn't fill the memory from high-order to low-order when octal escape occur?

Yet it's not the whole story, I'll show something more interesting here

unsigned int k = '0\101\1001'; // k = 0x31403041 memory[low->high] [0x41 0x30 0x40 0x31] ??what the hell
unsigned int l = '0\1011\100'; // l = 0x40304131 memory[low->high] [0x31 0x41 0x30 0x40] ??what the hell again

So far, I'm totally lost. I can't even conclude a simple rule from these test cases.

Doesn't anybody know something about this issue? Thanks.

nir
  • 11
  • 1

1 Answers1

0

I see no contradiction here. When you use character literals to assign values to int types, they are treated the same way as assigning to wchar_t. The only difference is, int occupies four bytes and wchar_t has two bytes (typically). See the following example in the documentation page:

wchar_t w1 = L'\100';   // L'@'
wchar_t w2 = L'\1000';  // C4066 L'@', 0 ignored 
wchar_t w3 = L'\009';   // C4066 L'\0', 9 ignored
wchar_t w4 = L'\089';   // C4066 L'\0', 89 ignored
wchar_t w5 = L'\qrs';   // C4129, C4066 L'q' escape, rs ignored
wchar_t w6 = L'\x0050'; // L'P'
wchar_t w7 = L'\x0pqr'; // C4066 L'\0', pqr ignored
polfosol ఠ_ఠ
  • 1,840
  • 26
  • 41
  • Thanks for that. However, that's not what I want. I do know that int occupies four bytes and wchar_t has two bytes, however, I need to know, when I write '0\101\1001', what's it like in memory. It seems that VS2013 will put [0x41, 0x30, 0x40, 0x31] into the memory from low-address to high-address, which is not same as the MSDN describes. – nir Apr 30 '16 at 04:15
  • It has been a while. But I just read [this good answer](http://stackoverflow.com/a/10220539/5358284) and remembered your question. Just in case though – polfosol ఠ_ఠ May 11 '16 at 11:49