Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6716503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:44:30+00:00 2026-05-26T08:44:30+00:00

The following C code, compiled and run in XCode: UInt16 chars = ‘ab’; printf(\nchars:

  • 0

The following C code, compiled and run in XCode:

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

prints ‘ba’, rather than ‘ab’.

Why?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:44:30+00:00Added an answer on May 26, 2026 at 8:44 am

    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 viewed chars as an array, it’d be chars[0] = 'b' and chars[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:

    Multi-character constants (e.g. ‘xy’) are valid, although rarely
    useful — they let one store several characters in an integer (e.g. 4
    ASCII characters can fit in a 32-bit integer, 8 in a 64-bit one).
    Since the order in which the characters are packed into one int is not
    specified, portable use of multi-character constants is difficult.

    So it appears the 'ab' multi-character constant format should be avoided in general.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following code, when compiled and run with g++, prints '1' twice, whereas I expect
if I compile (under G++) and run the following code it prints Foo::Foo(int). However
the following code can be compiled correctly on both VC or gcc: char *str
Why whenever I compile and run the following code in Visual Studio 2008: double
When I compile the following code, I only see the error during Run-time which
I have following code snippet that i use to compile class at the run
I have the following code compiled by gcc: #include <iostream> using namespace std; class
I compiled the following code as a shared library using g++ -shared ... :
the following piece of C++ code compiled two years ago in a suse 10.1
The following code is generating warning C6284 when compiled with /analyze on MSVC 2008

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.