{"id":7821,"date":"2026-10-17T00:01:10","date_gmt":"2026-10-17T07:01:10","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7821"},"modified":"2026-10-10T11:13:32","modified_gmt":"2026-10-10T18:13:32","slug":"peeking-at-data-stored-in-a-union","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7821","title":{"rendered":"Peeking at Data Stored in a Union"},"content":{"rendered":"<p>A C language union is a shared data space accessed via different data types. This feature can be handy, providing that you tag the union data as was seen in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7783&#038;preview=true\">this Lesson<\/a>. But I&#8217;m after exploiting a union for a more abusive yet useful purpose.<br \/>\n<!--more--><br \/>\nMy goal is to exploit a union&#8217;s weakness by accessing a specific data type through another data type to reveal out real numbers are stored in binary. The union provides a tool I can use to &#8220;peek under the hood&#8221; and see the binary format directly. But first, consider the following example:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_10_17-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_10_17-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    union real {\r\n        float a;\r\n        long b;\r\n        char c[8];\r\n    };\r\n    union real numb;\r\n    int x;\r\n\r\n    numb.a = 10.0;\r\n\r\n    printf(\"The value of numb.a is %f\\n\",numb.a);\r\n    printf(\"The value of numb.b is 0x%0lX\\n\",numb.b);\r\n    printf(\"Union numb occupies %lu bytes:\",sizeof(numb));\r\n    for( x=0; x&lt;8; x++ )\r\n        printf(\" %02X\",numb.c[x]);\r\n    putchar('\\n');\r\n\r\n    return 0;\r\n}<\/pre>\n<p>In the code, union <code>real<\/code> contains three data types all sharing the same storage: a <em>float<\/em>, a <em>long<\/em>, and a four-<em>char<\/em> array.<\/p>\n<p>The program assigns the value 10.0 to <em>float<\/em> variable <code>numb.a<\/code>. This same value is output as a <em>long<\/em> stores it (in <code>numb.b<\/code>) and then the individual bytes are output as stored in the <em>char<\/em> array <code>numb.c[]<\/code>:<\/p>\n<pre>The value of numb.a is 10.000000\r\nThe value of numb.b is 0x41200000\r\nUnion numb occupies 8 bytes: 00 00 20 41 00 00 00 00<\/pre>\n<p>Variable <code>numb.a<\/code> is assigned real number 10.0. When read as a <em>long int<\/em>, the value is 0x41200000. This value reflects the way real numbers are stored in binary, so it&#8217;s not the same as 0x0A or 10 decimal.<\/p>\n<p>The output for <em>char<\/em> array <code>numb.c[]<\/code> is interesting to me. It shows how the bytes appear in memory for the real number 10.0. But the values are output backwards as a <em>long int<\/em>, which reflects the way CPUs store data.<\/p>\n<p>It&#8217;s all fascinating stuff, but what intrigues me the most is how binary data represents a real number. The value 10 is stored as 0x41200000. So what&#8217;s going on with those bits that cause them to be interpreted as 10.0? And what about the precision or accuracy of the value? How is that accounted for?<\/p>\n<p>In next week&#8217;s Lesson I cover the mystery of how real numbers are stored in binary.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What you see in a union is based on how the data is interpreted. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7821\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-7821","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7821","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7821"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7821\/revisions"}],"predecessor-version":[{"id":7838,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7821\/revisions\/7838"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7821"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7821"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7821"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}