U should know that union's elements share memory alloted through the largest data type like in this case 10 bytes is alloted and is shared by all the union elements.
So in union x :- last element modified is c, so that's why it is printing garbage values for int and char[] elements... actually if u convert float value in binary then u will see that what's its actual integer value is? same case for char[] in union y : last modified element is integer thats y it is printing 100 for integer... ascii value of 'd' is 100 thats y printing 'd' for char[] and 0.000 for float On Mon, Aug 22, 2011 at 4:53 PM, vartika <[email protected]>wrote: > #include<stdio.h> > #include<string.h> > int main() > { > typedef union > { > int a; > char b[10]; > float c; > } > Union; > > Union x,y = {100}; > x.a = 50; > strcpy(x.b,"hello"); > x.c = 21.50; > > printf("Union x : %d %s %f \n",x.a,x.b,x.c ); > printf("Union y :%d %s%f \n",y.a,y.b,y.c); > return 0; > } > > Someone pls explain the output > > -- > You received this message because you are subscribed to the Google Groups > "Algorithm Geeks" group. > To post to this group, send email to [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > > -- **Regards SAGAR PAREEK COMPUTER SCIENCE AND ENGINEERING NIT ALLAHABAD -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
