C Programming - Declarations and Initializations - Discussion

Discussion Forum : Declarations and Initializations - Find Output of Program (Q.No. 10)
10.
What is 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
None of these
Answer: Option
Explanation:

printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i); It prints the value of u.ch[0] = 3, u.ch[1] = 2 and it prints the value of u.i means the value of entire union size.

So the output is 3, 2, 515.

Discussion:
77 comments Page 6 of 8.

Wikiok said:   1 decade ago
#include<stdio.h>
union
{
int a;
char c[2];
} myu;
int main()
{
printf("%d",sizeof(myu)); //Size of the union in memory. 4
return 0;
}
-----------------
sizeof(union) = sizeof( largest element )

Anu said:   1 decade ago
Please could anyone explain me this one I cant get it how. I gives this value?and how to calculate the size of union?

Gangadhararao said:   1 decade ago
What is the output of the program given below:

#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;
}

Explain in detail about the output.

Harwinder singh said:   1 decade ago
But the union can store the value of a single element at a time,

Allocation of new value will delete previous one.

And how calculate size of entire union?

Soma said:   1 decade ago
It is a character array of size 2 byte because char in c is of 1 byte.

The value to be filled in the array field at index 0 is three.

And three is represented as 11 in binary and you have represent 11 in 8-bits, therefore the last two bit is set to 1 and rest zero at array index zero. And at array index one fill 2 which is represented by 10 and the rest field set to 0.

Now calculate the positional weight of the whole union. You will get the ans.

Soma said:   1 decade ago
Please explian how to find out the size of union.

Gopi said:   1 decade ago
Anybody give brief explanation to calculate the union size?

Nguyen Tien Thinh said:   1 decade ago
A structure is a collection of items of different types; and each data item will have its own memory location. Where as only one item within the union can be used at any time, because the memory allocated for each item inside the union is in a shared memory location i.e., only one memory location will be shared by the data items of union.

Size of union will be the size of the biggest variable.

Why do we need Union in the first place?

Sometimes we may not need the data of all the (related) data items of a complex data structure and be storing/accessing only one data item at a time. Union helps in such scenarios.

e.g.,
typedef union
{
int Wind_Chill;
char Heat_Index;
} Condition;

typedef struct
{
float temp;
Condition feels_like;
} Temperature;

Wind Chill is only calculated when it is cold and heat index is used only when it is hot. There is no need for both of them at the same time. So when we specify the temp, feels_like will have only one value - either wind chill or heat index, but not both.

The following simple program illustrate the above explanation:
% cat structunion.c
#include <stdio.h>
#include <stdlib.h>

typedef union
{
int Wind_Chill;
char Heat_Index;
} Condition;

typedef struct
{
float temp;
Condition feels_like;
} Temperature;

void main()
{
Temperature *tmp;

tmp = (Temperature *)malloc(sizeof(Temperature));

printf("\nAddress of Temperature = %u", tmp);
printf("\nAddress of temp = %u, feels_like = %u",
&(*tmp).temp, &(*tmp).feels_like);
printf("\nWind_Chill = %u, Heat_Index= %u\n",
&((*tmp).feels_like).Wind_Chill, &((*tmp).feels_like).Heat_Index);
}

% cc -o structunion structunion.c

% ./structunion
Address of Temperature = 165496
Address of temp = 165496, feels_like = 165500
Wind_Chill = 165500, Heat_Index= 165500

Jay Patel said:   1 decade ago
Here ch[0]=3 the binary value of 3 = 00000011=011 and ch[1]=2 the binary value of 2=010=00000010.

Now we can call the size of union i.e, 00000010 00000011.

See the fig so 1*1+2*1+512*1=515.

Moon said:   1 decade ago
Please give an example and give a description? Why we use struct and union ? where can we use it (with an example) ?


Post your comments here:

Your comments will be displayed after verification.