C Programming - Structures, Unions, Enums - Discussion

Discussion Forum : Structures, Unions, Enums - Find Output of Program (Q.No. 1)
1.
What will be the output of the program ?
#include<stdio.h>

int main()
{
    union a
    {
        int i;
        char ch[2];
    };
    union a u;
    u.ch[0]=3;
    u.ch[1]=2;
    printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
    return 0;
}
3, 2, 515
515, 2, 3
3, 2, 5
515, 515, 4
Answer: Option
Explanation:

The system will allocate 2 bytes for the union.

The statements u.ch[0]=3; u.ch[1]=2; store data in memory as given below.

Discussion:
40 comments Page 4 of 4.

Ramya said:   1 decade ago
hey i cant get ur answer ,plz explain me how the value of u.i is assigned

Sai said:   1 decade ago
@karthik i cant get you

Please explain in detail

(2)(3) --> 0000001000000011 --> 515.

how it came..

I am a begineer, please help me some one.

Saranya devi said:   1 decade ago
Thanks karthick.

Narmadha said:   1 decade ago
o k k get the binary value k k

Bhanu said:   1 decade ago
How you get that value? please give explain in detail.

Aniruddh said:   1 decade ago
Kartik you are absolutely right.

Uma said:   1 decade ago
thank u

Karthik said:   1 decade ago
u.ch[0]=3;
u.ch[1]=2;

Now u.i is calculated as

(2)(3) in binary form which is equal to 515.

(2) --> 00000010; (3) --> 00000011

(2)(3) --> 0000001000000011 --> 515.

Pranay said:   1 decade ago
Union takes memory, according the largest data types.

Pavani said:   2 decades ago
How to get the value of u.i ?


Post your comments here:

Your comments will be displayed after verification.