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

  • Home
  • SEARCH
  • 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 7667917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T15:13:31+00:00 2026-05-31T15:13:31+00:00

I got the output 0 2 for this program….. but don’t know why? Please

  • 0

I got the output 0 2 for this program…..
but don’t know why?
Please explain i think only int i is initialized with 512.
But how ch[1] got the value 2.

#include <stdio.h>
int main()
{
    union a /* declared */
    {
        int i;   char ch[2];
    };
    union a z = { 512 };   

    printf("%d%d", z.ch[0], z.ch[1]);
    return 0;
}
  • 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-31T15:13:33+00:00Added an answer on May 31, 2026 at 3:13 pm

    Union declaration means that all its members are allocated the same memory. So your int i and char ch[2] are referencing the same memory space — in other words, they are aliased. Whenever you change one, you will change the other as well.

    Now, assuming your ints are 32-bit wide and you’re on a little-endian system like x86, i = 512 (512 == 0x00000200) actually looks like this in memory:

    0x00  0x02  0x00  0x00.
    

    with the first two values corresponding directly to the 2-character array:

    ch[0] ch[1]
    

    So you get ch[0] == 0x0 and ch[1] == 0x02.

    Try setting your i = 0x1234 and see what effect it will have on your character array.

    Based on your question, it’s possible that you may want to use a struct instead of union — then its members would be allocated in memory sequentially (one after the other).

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

Sidebar

Related Questions

I got this in my output. The program still runs and finishes normally but
I got this output when running sudo cpan Scalar::Util::Numeric jmm@freekbox:~/bfwsandbox/sa/angel/astroportal/dtu8e/resources$ sudo cpan Scalar::Util::Numeric [sudo]
This is the xml I got as an output from the WriteXmlString() of Infragistics
While answering this question, I got these confusing results: double d = 0.49999999999999990d; //output
How do I execute an output piped from a bash script? I got this
I've got the following code to output a list of items from amazon, but
I got a question about this program, it says: The FizzBuzz Challenge: Display numbers
I wrote this simple C program: int main() { int i; int count =
I compile a Qt program with C++, and I got this error message: cannot
I was just doing some stuff and wrote this program. I got the following

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.