Home » C Programming » Unions » Question
  1. Which of the following is the correct output for the program given below?

    #include<studio.h>
    int main()
    {
    union a
    {
    short int i;
    char ch[1];
    };
    union a u;
    u.ch[0] = 10;
    u.ch[1] = 20;
    printf("%d %d %d\n", u.ch[0], u.ch[1], u.i);
    return 0;
    }
    1. 10 20 5130
    2. 5130 10 20
    3. 10 20 50
    4. None of the above
Correct Option: A

The system will allocate 2 bytes for the union.
First byte for ch[0] i.e. for 10 = 00001010
Second byte for ch[1] i.e. for 20 = 000010100

Combining these 2 we get 00001010 000010100 which is equivalent to 5130 in decimal.

So answer will be 10 20 5130



Your comments will be displayed only after manual approval.