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 6 of 8.
Akanksha said:
9 years ago
Why including only 512?
Kindly help by explaining this.
Kindly help by explaining this.
Rajat said:
9 years ago
When we are changing char array to short array it's not doing the same thing. In my compiler size of int is 4 and short is 2.
Punya said:
9 years ago
Please check by running the same program with c compiler, the Garbage value will be printed for i.
Kavya said:
9 years ago
How/why we get 512 and 515? Please explain me.
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.
Sanju said:
9 years ago
Every time I run the program, it didn't give 515 for me. It's giving some garbage value. Please help me.
Rashmi said:
9 years ago
Why u.i would print size of the union? What exactly u.i means?
Ayush said:
9 years ago
How can we get the size of the entire union by u.i?
Deepika said:
9 years ago
union a u;
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
Can anyone explain these both lines?
printf("%d, %d, %d\n", u.ch[0], u.ch[1], u.i);
Can anyone explain these both lines?
Naziya said:
9 years ago
Explanation to the given question is as follows. As we know union size is the biggest size of its element, hence integer is considered to be the biggest.
Here integer is considered to be 4 bytes which are equal to 32 bits (GCC compiler). The memory allocation is as follows. 0000000000000000000000010000000011.
Hence from the above figure u.ch[0] = 3 & u.ch[1]=2.
As the character is of 1 byte.
So in that 4 bytes itself u.ch[0] and u.ch[1] can be occupied, as we are considering the biggest element to b integer, in other words, memory is shared.
So when we see u.i value is equivalent to 512 (the value at 10th bit) +2 (the value at 1st bit) +1 (the value at 0th bit). Therefore 512 + 2 + 1 = 515.
Here integer is considered to be 4 bytes which are equal to 32 bits (GCC compiler). The memory allocation is as follows. 0000000000000000000000010000000011.
Hence from the above figure u.ch[0] = 3 & u.ch[1]=2.
As the character is of 1 byte.
So in that 4 bytes itself u.ch[0] and u.ch[1] can be occupied, as we are considering the biggest element to b integer, in other words, memory is shared.
So when we see u.i value is equivalent to 512 (the value at 10th bit) +2 (the value at 1st bit) +1 (the value at 0th bit). Therefore 512 + 2 + 1 = 515.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers