[Davis@localhost UNS]$ gcc U_bc.c
[Davis@localhost UNS]$ ./a.out
a=d i=100 f=0.000000
sizeof(u)=4
[Davis@localhost UNS]$ cat U_bc.c
int main()
{
        union test
        {
                char ch;
                float f;
                int i;
        };
//union test u={'a',21,34.65}; gives a warning excess elements
union test u={100.23};
printf("a=%c i=%d f=%f\n",u.ch,u.i,u.f);

printf("sizeof(u)=%d\n",sizeof(u));
return 0;
}

in the above program it is interpreting the float value as char & int but float value is not getting interpreted.

Recommended Answers

All 3 Replies

initialize as

union test u;
u.f = 100.23;

initialize as

union test u;
u.f = 100.23;

i know the basic initialization of union.

my doubt is why format specifier is not able to convert it in to float value.

bcz the value stored in it is a float value.

A union initializer pertains to the first named member.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.