#include said:
{ int I;
char c[2];
} };
main() int main(void)
{
union un x;
x.c[0]=10;
x.c[1]=1;
printf("%d",.i);
printf("%d\n",x.I);
return 0;> }
Thank you very much!I have something wrong with the procedure, as you
corrected by the right .
Your code still does not compile!
#include <stdio.h>
union un
{
int I;
char c[2];
}
Change that to:
};
int main(void)
{
union un x;
x.c[0]=10;
x.c[1]=1;
printf("%d",.i);
That statement won't compile and requires a diagnostic. Just delete
it, since your following statement is correct.
printf("%d\n",x.I);
return 0;
}
I notice that you changed your posted name from "?????" to "???". I
have a feeling that if you change your posted name to simply "?", your
code will compile
All kidding aside, I'll tell you this ... in my 15+ years of
programming in C, I have never--not once--legitimately used a union,
because I've never needed to legitimately use one.
I've dealt with unions declared by compilers, which most compilers
inevitably seem to use (and I can live with that), and I've dealt with
fellow programmers who've declared unions (IMHO, unnecessarily). But
I've never found the need to legitimately use unions myself.
My advice to you is to understand unions, but to never use them in
your own code. In your example, I can tell you that you will not get
the results you expect on a lot of implementations, such as those in
which sizeof(int) != 2 (many if not most) and on those in which the
Endianness is Big when yours is Little or vice versa.
Best regards