Note that there are some explanatory texts on larger screens.

plurals
  1. POIs type-punning through a union unspecified in C99, and has it become specified in C11?
    text
    copied!<p>A number of answers for the Stack Overflow question <a href="https://stackoverflow.com/questions/11638091/getting-the-ieee-single-precision-bits-for-a-float">Getting the IEEE Single-precision bits for a float</a> suggest using a <code>union</code> structure for type punning (e.g.: turning the bits of a <code>float</code> into a <code>uint32_t</code>):</p> <pre><code>union { float f; uint32_t u; } un; un.f = your_float; uint32_t target = un.u; </code></pre> <p>However, the value of the <code>uint32_t</code> member of the union appears to be unspecified according to the C99 standard (at least draft n1124), where section 6.2.6.1.7 states: </p> <blockquote> <p>When a value is stored in a member of an object of union type, the bytes of the object representation that do not correspond to that member but do correspond to other members take unspecified values.</p> </blockquote> <p>At least one footnote of the C11 n1570 draft seems to imply that this is no longer the case (see footnote 95 in 6.5.2.3):</p> <blockquote> <p>If the member used to read the contents of a union object is not the same as the member last used to store a value in the object, the appropriate part of the object representation of the value is reinterpreted as an object representation in the new type as described in 6.2.6 (a process sometimes called ‘‘type punning’’). This might be a trap representation.</p> </blockquote> <p>However, the text to section 6.2.6.1.7 is the same in the C99 draft as in the C11 draft.</p> <p>Is this behavior actually unspecified under C99? Has it become specified in C11? I realize that most compilers seem to support this, but it would be nice to know if it's specified in the standard, or just a very common extension.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload