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 1 of 4.

Vijay said:   3 years ago
Here the concept is union memory management. union memory size is largest data member.

Here
data members are : int i ; char a[2];
largest data member is int ie 4bytes.
note := 1byte=8bits => 1byte= 00000000.

4byte := 00000000 00000000 00000000 00000000.

for char u.a[0]=3; => uses first byte in memory
ie , 00000000 00000000 00000000 " 00000011 "
for char u.a[1]=1 ; => uses second byte(system allocates or selects like this).
ie , 00000000 00000000 "00000001" 00000011
whenever we are printing u.a[1] points to its 1 byte and gives 3
Similarly u.a[2] points to the second byte give 1.

when printing u.i, it is not initialized so it took the first 4 bytes ie entire memory

ie "00000000 00000000 00000001 00000011" => its value in binary is 515
2^8 + 2^1 + 2^0 =512+2+1 =515
So,u[i] prints 515.
(6)

Dash said:   1 decade ago
main()
{
union a{
int i;
char ch[2];
union a u;
u.ch[0]=3;
u.ch[1]=2;
here as we know the max size will be allocated is 2 bytes as size of character array is 2. Each value first represented in binary form and then stored in alloted memory space.This memory space again subdivided into two depending on no.of characters, as here two so(each having 8bits for storage).

So now according to binary display it is always from left to
right so the storing for characters will start from left which will be followed towards right consecutively.

Illustration:

2^15..........2^9 2^8 2^7 2^6...........2^1 2^0
| | | |
1 0 1 1
so u.ch[0]=3.
u.ch[1]=2.
u.i=2^9+2^1+2^0(summation of memory locations containing 1).
=512.

Thank you.

Sundar said:   1 decade ago
@Raju Kumar:

In TurboC it prints 0, 0, 0, 0 (as expected).

But in GCC it prints 0, 0, 0, 134217728 (last value is unexpected).

It is due to the platform dependency of compilers.

Because for 'int' TurboC (under DOS) reserves two bytes only. But in Linux GCC reserves 4 bytes.

You have initialized/assigned only three bytes (u.ch[0], u.ch[1], u.ch[2]) of the reserved bytes to 0 (zero). The remaining one byte (1 out of 4) not yet assigned with any value. So it may have some junk/garbage value.

So while printing the u.i, it prints the garbage value.

If the change your code for union as give below, the above problem will not occur.

union a
{
short i; // change 'int' --> 'short'
char ch[3];
};

Hope this will help you. Have a nice day!

Arpeeta Halder said:   10 years ago
In the above question:

union a
{
int i;
char ch[2];
};

Here, memory is allocated for int i, i.e 2 bytes now.

u.ch[0]=3;
u.ch[1]=2;

They are stored in memory allocated for int i (as char are of 1 bytes) they are placed in each bytes of integer i, now when we print.

printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;

Then, first u.ch[0] is printed then u.ch[1] then u.i where u.ch[0] then u.ch[1] from right to left is stored in memory allocated for u.i is saved.

Raju Kumar said:   1 decade ago
#include<stdio.h>

int main()
{
union a
{
int i;
char ch[3];
};
union a u;
u.ch[0]=0;
u.ch[1]=0;
u.ch[2]=0;
printf("%d, %d, %d, %d\n", u.ch[0], u.ch[1],u.ch[2], u.i);
return 0;
}
output:
0, 0, 0, 134217728


Now, I applied same approach as previous program but last ans should be zero but i got somewhat other than that what's the reason, can anyone help me .

Ashish said:   1 decade ago
@Sundar

why 20 comes as value of u.ch[2] in output?
I understand about garbage value. cant understand about u.ch[2]

#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, %d\n", u.ch[0], u.ch[1], u.ch[2], u.i);
return 0;
}
Ans is 3, 2, 20, some garbage value.

Zdd said:   8 years ago
#include<stdio.h>

int main()
{
union a
{
short int i;
char ch[2];
};
union a u;
u.ch[0]='a';
u.ch[1]='b';

printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}

Its output is:97, 98, 25185.

if I change 'short int' to 'char',then the output is; 97, 98, 97.

Noel said:   7 years ago
Agree @Raju Kumar.

If I make the data types in the union ( int i; & char ch[2];) match (both int or both char, etc.) then the number of bytes match and I get a predictable and expected value for u.i. That is, it matches the value of what is in the ch[0] memory location.
(1)

Akshata said:   8 years ago
#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, %d\n", u.ch[0], u.ch[1], u.ch[2], u.i);
return 0;
}

Getting ans as 3, 2, 110, -143785469.

How that 110 came? Please help

Akash said:   1 decade ago
@Ramkumar.

It depends if you print using %d then it will print the ASCII value of a and b or if you print using %c it will print actual character i.e a and b.

And if you print u.i it will print the 25185 (calculate as same process as above discuss).


Post your comments here:

Your comments will be displayed after verification.