{"id":7752,"date":"2026-09-05T00:01:48","date_gmt":"2026-09-05T07:01:48","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7752"},"modified":"2026-08-29T12:42:30","modified_gmt":"2026-08-29T19:42:30","slug":"keeping-up-with-the-booleans","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7752","title":{"rendered":"Keeping Up with the Booleans"},"content":{"rendered":"<p>As a programmer well-versed in Assembly Language, I&#8217;m all too familiar with the bits that populate a byte. At the low, low level you find plenty of tools to do bit manipulation. The C language has bitwise operators as well, which remains one of its advantages over the newer, trendier programming languages. So, how does all this fun activity cross over into the realm of the <em>_Bool<\/em> or <em>bool<\/em> data type in C?<br \/>\n<!--more--><br \/>\nI shall use the more apropos <em>bool<\/em> in this Lesson. Primarily because it&#8217;s a new data type with the C23 standard, but it&#8217;s also the way the <code>stdbool.h<\/code> header sets it up as mentioned in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7739\">last week&#8217;s Lesson<\/a>.<\/p>\n<p>In Assembly, I can use binary digits to concoct certain values. For example, in the following code, a <em>bool<\/em> array consists of what I believe to be eight binary digits.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_09_05-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2026_09_05-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdbool.h&gt;\r\n\r\nint main()\r\n{\r\n    bool data[] = { 0, 0, 1, 0, 0, 0, 0, 1 };\r\n\r\n    printf(\"%c\\n\",(char)data);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Array <code>data[]<\/code> contains eight one\/zero values of the <em>bool<\/em> type. This declaration is legit, but it turns out it doesn&#8217;t build a byte as it would according to my Assembly language brain. If it did, the <em>printf()<\/em> statement would output the result as an 8-bit value, a <em>char<\/em> (character), ASCII code 0x21 which represents the ! (bang). Instead, here is the output on one of my systems:<\/p>\n<pre>d<\/pre>\n<p>I see no output when the program is run on another system. Regardless, a compiler warning is issued explaining that the <code>data[]<\/code> array is technically composed of integers and not true binary digits. This result is something I suspected: How is binary data stored as a <em>bool<\/em>? Are they truly individual bits or are they integers wherein only one digit is considered valid?<\/p>\n<p>The following code adds two statements to help describe what&#8217;s going on with the <em>bool<\/em> in C:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_09_05-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2026_09_05-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdbool.h&gt;\r\n\r\nint main()\r\n{\r\n    bool data[] = { 0, 0, 1, 0, 0, 0, 0, 1 };\r\n\r\n    printf(\"Size of bool: %lu\\n\",sizeof(bool));\r\n    printf(\"Size of 'data': %lu\\n\",sizeof(data));\r\n    printf(\"%c\\n\",(char)data);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>This update to the code uses the <code>sizeof(bool)<\/code> operator to return the number of bytes occupied by a <em>bool<\/em> value. I suppose the result could be 0.125 (<sup>1<\/sup>\/<sub>8<\/sub>), but the value generated is 1, as shown in the output:<\/p>\n<pre>Size of bool: 1\r\nSize of 'data': 8\r\n&nbsp;\r\n<\/pre>\n<p>Most telling in the output is the size of the <code>data[]<\/code> array: It&#8217;s 8 bytes long. The Boolean values are stored as <em>char<\/em> data types, technically a small integer.<\/p>\n<p>One more modification as long as I&#8217;m messing around:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_09_05-Lesson-c.c\" rel=\"noopener\" target=\"_blank\">2026_09_05-Lesson-c.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdbool.h&gt;\r\n\r\nint main()\r\n{\r\n    bool data = 0x21;\r\n\r\n    printf(\"Size of bool: %lu\\n\",sizeof(bool));\r\n    printf(\"Size of 'data': %lu\\n\",sizeof(data));\r\n    printf(\"%c\\n\",(char)data);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Rather than build a <em>bool<\/em> array, this update to the code assigns <em>bool<\/em> value <code>data<\/code> to 0x21 (ASCII code for !). Here is output from a sample run:<\/p>\n<pre>Size of bool: 1\r\nSize of 'data': 1\r\n&nbsp;\r\n<\/pre>\n<p>The <em>printf(<\/em>) statement doesn&#8217;t output the ! character. Internally, the value 0x21 is stored, but only the far right bit is read: one. This result is output, but you can&#8217;t see ASCII code 0x01. Instead, I ran the program through the <em>hexdump<\/em> utility to see this:<\/p>\n<pre>00000000&nbsp;&nbsp;53&nbsp;69&nbsp;7a&nbsp;65&nbsp;20&nbsp;6f&nbsp;66&nbsp;20&nbsp;&nbsp;62&nbsp;6f&nbsp;6f&nbsp;6c&nbsp;3a&nbsp;20&nbsp;31&nbsp;0a&nbsp;&nbsp;|Size&nbsp;of&nbsp;bool:&nbsp;1.|\r\n00000010&nbsp;&nbsp;53&nbsp;69&nbsp;7a&nbsp;65&nbsp;20&nbsp;6f&nbsp;66&nbsp;20&nbsp;&nbsp;27&nbsp;64&nbsp;61&nbsp;74&nbsp;61&nbsp;27&nbsp;3a&nbsp;20&nbsp;&nbsp;|Size&nbsp;of&nbsp;'data':&nbsp;|\r\n00000020&nbsp;&nbsp;31&nbsp;0a&nbsp;<span style=\"color:red\">01<\/span>&nbsp;0a&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;|1...|\r\n00000024<\/pre>\n<p>The value 0x01 is output (above in <span style=\"color:red\">red<\/span>).<\/p>\n<p>In conclusion, while a <em>bool<\/em> data type represents a binary value of one or zero, the data is stored in a byte (<em>char<\/em>). Only the far right digit in the byte is significant. But the takeaway is that an array of 8 <em>bool<\/em> values occupies 8 bytes of memory not 8 bits.<\/p>\n<p>I continue my exploration of <em>bool<\/em> values in next week&#8217;s Lesson.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What can you do with a Boolean value? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7752\">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-7752","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\/7752","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=7752"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7752\/revisions"}],"predecessor-version":[{"id":7770,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7752\/revisions\/7770"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7752"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7752"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7752"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}