https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71560
Bug ID: 71560 Summary: union compound literal initializes wrong union field Product: gcc Version: 7.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c Assignee: unassigned at gcc dot gnu.org Reporter: vries at gcc dot gnu.org Target Milestone: --- Consider test.c: ... #include <stdio.h> union u { int c; float f; }; int main (void) { float f = 2.0; union u u; u = (union u){f}; printf ("%f\n", u.f); return 0; } ... This gives the result: ... $ gcc test.c $ ./a.out 0.000000 ... Here ( https://gcc.gnu.org/onlinedocs/gcc/Compound-Literals.html ) I read: ... Compound literals for scalar types and union types are also allowed, but then the compound literal is equivalent to a cast. ... So I'd expect the outcome to be the same as for: ... #include <stdio.h> union u { int c; float f; }; int main (void) { float f = 2.0; union u u; u = (union u)f; printf ("%f\n", u.f); return 0; } ... and that gives the expected: ... $ gcc test.c $ ./a.out 2.000000 ... AFAICT, the problem is introduced by the front-end, which interprets union init as initializing the int rather than the float field: ... { float f = 2.0e+0; union u u; float f = 2.0e+0; union u u; u = <<< Unknown tree: compound_literal_expr union u D.2241 = {.c=(int) f}; >>>; printf ((const char * restrict) "%f\n", (double) u.f); return 0; } return 0; ...