Endianness — why do chars put in an Int16 print backwards?

后端 未结 4 1013
一向
一向 2021-01-25 05:17

The following C code, compiled and run in XCode:

UInt16 chars = \'ab\';
printf(\"\\nchars: %2.2s\", (char*)&chars);

prints \'ba\', rather t

4条回答
  •  醉酒成梦
    2021-01-25 05:32

    Multicharacter character literals are implementation-defined:

    C99 6.4.4.4p10: "The value of an integer character constant containing more than one character (e.g., 'ab'), or containing a character or escape sequence that does not map to a single-byte execution character, is implementation-defined."

    gcc and icl print ba on Windows 7. tcc prints a and drops the second letter altogether...

提交回复
热议问题