Declaring variable without initializing it will have a garbage value so in this program I am using a union:
union un
{
int value;
char c;
};
int main()
{
un x;
x.c = 'A';
cout << x.value << endl; // garbage: ok
}
above it is ok when printing value it produces a garbage value as long as we didn't initialize it.
but look at this program:
union un
{
int value;
char c;
}r;
int main()
{
r.c = 'A';
cout << r.value << endl; // 65! as we know the ASCII value of character 'A' is 65
}
so what is the difference between the two program above and why in the second one value
gets the value of c
?