{"id":7228,"date":"2025-11-08T00:05:00","date_gmt":"2025-11-08T08:05:00","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7228"},"modified":"2025-11-15T09:34:17","modified_gmt":"2025-11-15T17:34:17","slug":"finding-a-binary-date","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7228","title":{"rendered":"Finding a Binary Date"},"content":{"rendered":"<p>Last week&#8217;s <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7219\">post<\/a> was on 1101, which is a binary number! My inner nerd got so excited, I aimed to write code that locates and outputs all binary dates throughout the calendar year.<br \/>\n<!--more--><br \/>\nThe simple program presented last week output all 365 dates of the year, presenting them as 0101 through 1231. But these values are just output. To determine which are binary in nature, the values must be stored for examination. My choice is to save the date as a four-character string.<\/p>\n<p>I could write code that saves all the dates, say as an array of 365 strings. Such work isn&#8217;t necessary, however, as each date is easily checked one at a time as opposed to wasting overhead for storage. Even so, I wrote the code anyway:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_11_08-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2025_11_08-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int month,d,offset;\r\n    int days[12] = {\r\n        31, 28, 31, 30, 31, 30,\r\n        31, 31, 30, 31, 30, 31\r\n    };\r\n    char date[365][5];\r\n\r\n    <span class=\"comments\">\/* calculate and store dates *\/<\/span>\r\n    offset = 0;\r\n    for( month=0; month&lt;12; month++ )\r\n    {\r\n        for( d=0; d&lt;days[month]; d++ )\r\n        {\r\n            snprintf(date[offset],5,\"%02d%02d\",\r\n                    month+1,\r\n                    d+1\r\n                  );\r\n            offset++;\r\n        }\r\n    }\r\n\r\n    <span class=\"comments\">\/* output all dates *\/<\/span>\r\n    for(offset=0; offset&lt;365; offset++)\r\n        printf(\"%s\\n\",date[offset]);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The major update to this code from last week&#8217;s source code is changing the <em>printf()<\/em> statement to <em>snprintf()<\/em>, which stores the generated string in buffer <code>date[offset<\/code>].<\/p>\n<p>The <code>date[][]<\/code> array has room for 365 four-character dates (plus the null character). Variable <code>offset<\/code> tracks each date. I tried to calculate the offset value (representing an array element) by using the <code>month<\/code> and <code>d<\/code> variables, though this process proved too mathematically cumbersome.<\/p>\n<p>Here is the pointer version of this same code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_11_08-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2025_11_08-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n#include &lt;stdlib.h&gt;\r\n\r\nint main()\r\n{\r\n    int month,d,offset;\r\n    int days[12] = {\r\n        31, 28, 31, 30, 31, 30,\r\n        31, 31, 30, 31, 30, 31\r\n    };\r\n    char *date = NULL;\r\n\r\n    <span class=\"comments\">\/* allocate storage *\/<\/span>\r\n    date = malloc(365*5);\r\n    if( date==NULL )\r\n    {\r\n        fprintf(stderr,\"Can't allocate storage\\n\");\r\n        exit(1);\r\n    }\r\n\r\n    <span class=\"comments\">\/* calculate and store dates *\/<\/span>\r\n    offset = 0;\r\n    for( month=0; month&lt;12; month++ )\r\n    {\r\n        for( d=0; d&lt;days[month]; d++ )\r\n        {\r\n            snprintf((date+(offset*5)),5,\"%02d%02d\",\r\n                    month+1,\r\n                    d+1\r\n                  );\r\n            offset++;\r\n        }\r\n    }\r\n\r\n    <span class=\"comments\">\/* output all dates *\/<\/span>\r\n    for(offset=0; offset&lt;365; offset++)\r\n        printf(\"%s\\n\",date+(offset*5));\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Rather than allocate storage on-the-fly (which I coded originally because I&#8217;m dumb), this code allocates a huge chunk of memory to store the 365 4-character strings. Variable <code>offset<\/code> then tracks the depth within the buffer to store the string values. The expression is <code>date+(offset*5)<\/code>, which accesses a position inside the buffer to set the string. This same expression is used in the <em>printf()<\/em> statement to output the results.<\/p>\n<p>Though coding these two examples was fun, it&#8217;s not necessary to store every dang doodle date string for the final program. Rather, each string need only be presented to a function for examination to determine whether it contains only binary digits one and zero. Therefore, here is my final and best version of the code:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_11_08-Lesson-c.c\" rel=\"noopener\" target=\"_blank\">2025_11_08-Lesson-c.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int month,d;\r\n    int days[12] = {\r\n        31, 28, 31, 30, 31, 30,\r\n        31, 31, 30, 31, 30, 31\r\n    };\r\n    char date[5];\r\n\r\n    for( month=0; month&lt;12; month++ )\r\n    {\r\n        for( d=0; d&lt;days[month]; d++ )\r\n        {\r\n            snprintf(date,5,\"%02d%02d\",\r\n                    month+1,\r\n                    d+1\r\n                  );\r\n            printf(\"%s\\n\",date);\r\n        }\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Only one, 5-byte storage container is need: <code>date[]<\/code>. The <em>snprintf()<\/em> function stores each date in this container, then the date is output.<\/p>\n<p>For <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7238\">next week&#8217;s Lesson<\/a>, I add a function to examine the date to confirm whether it&#8217;s a valid binary value.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The year&#8217;s dates must be stored for proper binary examination. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7228\">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-7228","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\/7228","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=7228"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7228\/revisions"}],"predecessor-version":[{"id":7251,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7228\/revisions\/7251"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}