C union member gives particular/wrong value when other member is set to a new value. Why is this output in following code in C?
问题 #include <stdio.h> int main() { union Data { char str[20]; int i; float f; }data; data.i=20; data.f=220.5; printf("%d\n",(data.i)); return 0; } The output is:1130135552. I used gcc complier on Ubuntu 16.04 LTS. Can someone explain the output? The members data.i and data.f occupy same memory location, so output should be 220. But why output is 1130135552? 回答1: According to C11 Section 6.5.2.3 Footnote 95 If the member used to read the contents of a union object is not the same as the member