0

Output question

union Data{ int one;int two; }; int main() { union Data t; t.one=5; t.two=3; printf("%d\n", t.one); return 0; } Why is the output 3? Reason please?

1st Jan 2021, 12:19 PM
Achintya Raj
Achintya Raj - avatar
5 Answers
+ 3
unions use the same memory address for each value in it. And only one can be at the address at a time.
1st Jan 2021, 12:26 PM
Slick
Slick - avatar
+ 2
Wichever one you put in last is what matters. Its the value at the memory address that you call.
1st Jan 2021, 12:32 PM
Slick
Slick - avatar
+ 1
Ok thanks I will read it in detail 👍
1st Jan 2021, 12:33 PM
Achintya Raj
Achintya Raj - avatar
0
But t.one value is 5so ans should be 5
1st Jan 2021, 12:31 PM
Achintya Raj
Achintya Raj - avatar
0
And it is union so I think that it comes with the biggest member
1st Jan 2021, 12:33 PM
Achintya Raj
Achintya Raj - avatar