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;
}
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 3 of 8.
Bhanu said:
1 decade ago
It gives different output in gcc or MS vc++.
In gcc the int size is 4 bytes. In the code only 2 bytes are initialized (MSB 2bytes contains uninitialized data). So output is garbage data. To avoid this add
u.i = 0;
u.ch[0] = 3;
u.ch[1] = 2;
In gcc the int size is 4 bytes. In the code only 2 bytes are initialized (MSB 2bytes contains uninitialized data). So output is garbage data. To avoid this add
u.i = 0;
u.ch[0] = 3;
u.ch[1] = 2;
Neha said:
1 decade ago
I have a doubt that structure and union is almost same then why do we need union in first place if structure is already there? I know the difference between them but don't know the purpose of using union over structure? please anyone?
Ajay gupta said:
7 years ago
@All.
As explained that how it became 515 and I understood it. But why we need to calculate the size of the union while we are accessing the value of the variable I in the union u. why the size and how u, I is related to the size.
As explained that how it became 515 and I understood it. But why we need to calculate the size of the union while we are accessing the value of the variable I in the union u. why the size and how u, I is related to the size.
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 )
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 )
Avinash said:
1 decade ago
#include<stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
u.i=11;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}
Try output for this program.
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
u.i=11;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
return 0;
}
Try output for this program.
Ace said:
1 decade ago
#include<stdio.h>
int main()
{
union a
{
long int i, j;
int ch[2];
};
union a u;
u.ch[0] = 3;
u.i=1;u.j = 2;
u.ch[1] = 5 ;
printf( "%d, %d, %d, %d\n", u.ch[0], u.ch[1], u.j, u.i );
return 0;
}
int main()
{
union a
{
long int i, j;
int ch[2];
};
union a u;
u.ch[0] = 3;
u.i=1;u.j = 2;
u.ch[1] = 5 ;
printf( "%d, %d, %d, %d\n", u.ch[0], u.ch[1], u.j, u.i );
return 0;
}
Suman sagar said:
1 decade ago
Actually union size is the biggest size of its variable. But by using the instance of union if we access u.i it will indicate the value of union member/variable. But why it is giving the answer as 515?
Nidhi bhardwaj said:
9 years ago
But why are assuming that "i" will have size of union and not garbage value?
Because as I have studied about union, the most recent allocation is given the memory and rest all have garbage.
Because as I have studied about union, the most recent allocation is given the memory and rest all have garbage.
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.
Now we can call the size of union i.e, 00000010 00000011.
See the fig so 1*1+2*1+512*1=515.
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?
Allocation of new value will delete previous one.
And how calculate size of entire union?
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers