The following C code, compiled and run in XCode:
UInt16 chars = 'ab';
printf("\nchars: %2.2s", (char*)&chars);
prints ‘ba’, rather than ‘ab’.
Why?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That particular implementation seems to store multi-character constants in little-endian format. In the constant
'ab'the character'b'is the least significant byte (the little end) and the character'a'is the most significant byte. If you viewedcharsas an array, it’d bechars[0] = 'b'andchars[1] = 'a', and thus would be treated by printf as"ba".Also, I’m not sure how accurate you consider Wikipedia, but regarding C syntax it has this section:
So it appears the
'ab'multi-character constant format should be avoided in general.