G
Guest
Jack said:Ah, yes, I missed that these other bytes still may be uninitialized... (onMarcin said:Union un
{ int I;
char c[2];
}
After "fixing" your code to make it compiled:
- int 4 bytes
- char 1 byte, char[2] - 2 bytes
so, after union is created it is filled with trash. Then you assign a
values
but only to "char" components, the other two bytes of "int" component are
still filled with "trash". So final answer is "you will get unspecified
number".
an implementation with sizeof(int) != 2)
but
union un x;
x.I = 0;
x.c[0]=10;
x.c[1]=1;
printf("%d",x.I);
would still give some implementation/architecture dependant output.
No, it gives you undefined behavior. If you don't understand what
undefined behavior is, or why this program invokes it, you are not
qualified to answer questions on this group.
The program is guaranteed to print an unspecified value on
implementations where int has no trap representations, and even if it
weren't, the output (if any) would still be implementation/
architecture dependant. That's part of what "undefined" means.