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:
78 comments Page 5 of 8.
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) ?
Ravi said:
1 decade ago
Why we are assigning the 515 to the int i?
Why 'i' is not having any other value(like 0 or 1)?
Why 'i' is not having any other value(like 0 or 1)?
Naveen said:
1 decade ago
@Suman sagar.
Consider it has a memory cell.
These are 2 memory cell(1byte =8 bits) ,size of the union is 2memory cells u.ch[0] occupies 1st memory cell ie 1-128, u.ch[1] occupies 2nd memory cell ie 256-65536. value assigned to u.ch[0] is 3 indicate the in 1st memory cell, u.ch[1] is assigned 2 indicate that is 2nd memory cell look for u.ch[1] it is like starting 1-128 but 1-65536 is one memory cell as a whole for the union.
So according to u.ch[1] its value is 2.
But according to u[i] its value is 512 + 2+ 1.
Int occupies two memory cells whereas char occupies 1 memory cell.
Consider it has a memory cell.
These are 2 memory cell(1byte =8 bits) ,size of the union is 2memory cells u.ch[0] occupies 1st memory cell ie 1-128, u.ch[1] occupies 2nd memory cell ie 256-65536. value assigned to u.ch[0] is 3 indicate the in 1st memory cell, u.ch[1] is assigned 2 indicate that is 2nd memory cell look for u.ch[1] it is like starting 1-128 but 1-65536 is one memory cell as a whole for the union.
u.ch[2] view 128 64 32 .... 2 1 | 128 .. 4 2 1
u[i] view 65536 .. 1024 512 256 | 128 64.. 2 1
1 0 1 1
So according to u.ch[1] its value is 2.
But according to u[i] its value is 512 + 2+ 1.
Int occupies two memory cells whereas char occupies 1 memory cell.
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?
Theevan said:
1 decade ago
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);
I got these output...not the mentioned in the explanation.
3, 2, 1075053059.......why?
{
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);
I got these output...not the mentioned in the explanation.
3, 2, 1075053059.......why?
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?
Srujan reddy said:
1 decade ago
%d is a format specifier for integer. So when u use %d in printf() statement for a character it will print the ascii value of that character? Explain in detail how could the solution gain those output value.
union has two variables indeed. the one is an integer and a character sequence of 2.
So memory is allocated either for int variable or for character sequence. but could you access the all the variables of the union at a time?
union has two variables indeed. the one is an integer and a character sequence of 2.
So memory is allocated either for int variable or for character sequence. but could you access the all the variables of the union at a time?
Swetha92 said:
1 decade ago
I tried out this
#include<stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
u.ch[2] = 7;
printf("%d, %d, %d, %d\n", u.ch[0], u.ch[1], u.ch[2], u.i);
return 0;
}
I got the ans as 3,2,7,459267 and I also verified manually
But can u explain me why when char[2] declaration changed to char[3] gives result as 3,2,7,134676995
I mean I get the above result for the following program
#include<stdio.h>
int main()
{
union a
{
int i;
char ch[3];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
u.ch[2] = 7;
printf("%d, %d, %d, %d\n", u.ch[0], u.ch[1], u.ch[2], u.i);
return 0;
}
What does char[2] and char[3] declarations actually do to the program?
Pls explain me.
#include<stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
u.ch[2] = 7;
printf("%d, %d, %d, %d\n", u.ch[0], u.ch[1], u.ch[2], u.i);
return 0;
}
I got the ans as 3,2,7,459267 and I also verified manually
But can u explain me why when char[2] declaration changed to char[3] gives result as 3,2,7,134676995
I mean I get the above result for the following program
#include<stdio.h>
int main()
{
union a
{
int i;
char ch[3];
};
union a u;
u.ch[0] = 3;
u.ch[1] = 2;
u.ch[2] = 7;
printf("%d, %d, %d, %d\n", u.ch[0], u.ch[1], u.ch[2], u.i);
return 0;
}
What does char[2] and char[3] declarations actually do to the program?
Pls explain me.
Hemant7298 said:
1 decade ago
Thanks Soma.
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.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers