Unions


  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. View Hint View Answer Discuss in Forum

    The system will allocate 2 bytes for the union.

    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