#include<stdio.h>
int main()
{
union a
{
int i;
char ch[2];
};
union a z = {512};
printf("%d %d",z.ch[0],z.ch[1]);
return 0;
}
The output is: 0 2
Why is the output 0 2, when it should be some garbage value?
I am not sure why you expect the compiler to generate garbage for you, when you have just told it to initialize to i
to 512
. The least two significant bytes of 512 are 0
and 2
.
Implementation-specific behavior is not the same as garbage.