i was wondering whether union variable will be initialized like a structure variable or not...
#include<stdio.h>
int main(void)
{
struct a
{
int i;
char c;
};
struct a ob={4};
printf("%d",ob.c);
}
the above code gives 0 as output..
so when i is intialized c also gets intialized..
in the below code...if the union member integer also gets intialized with the character array this snippet would give the output 515...
(i verified it by allocating memory for the union variable using malloc..it works fine.)
#include<stdio.h>
int main(void)
{
union a
{
int i;
char c[2];
};
union a ob;
ob.ch[0]=3;
ob.ch[1]=2;
printf("%d",ob.i);
return 0;
}
but without allocating memory whether it could intialize the int i or not..(the hex value of int i in this code is set to 0x990203).
i think that 99 is the result that shows that the higher bits are not intialized..
am i correct?..