Finding a Binary Date

Last week’s post 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.

The 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.

I could write code that saves all the dates, say as an array of 365 strings. Such work isn’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:

2025_11_08-Lesson-a.c

#include <stdio.h>

int main()
{
    int month,d,offset;
    int days[12] = {
        31, 28, 31, 30, 31, 30,
        31, 31, 30, 31, 30, 31
    };
    char date[365][5];

    /* calculate and store dates */
    offset = 0;
    for( month=0; month<12; month++ )
    {
        for( d=0; d<days[month]; d++ )
        {
            snprintf(date[offset],5,"%02d%02d",
                    month+1,
                    d+1
                  );
            offset++;
        }
    }

    /* output all dates */
    for(offset=0; offset<365; offset++)
        printf("%s\n",date[offset]);

    return 0;
}

The major update to this code from last week’s source code is changing the printf() statement to snprintf(), which stores the generated string in buffer date[offset].

The date[][] array has room for 365 four-character dates (plus the null character). Variable offset tracks each date. I tried to calculate the offset value (representing an array element) by using the month and d variables, though this process proved too mathematically cumbersome.

Here is the pointer version of this same code:

2025_11_08-Lesson-b.c

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int month,d,offset;
    int days[12] = {
        31, 28, 31, 30, 31, 30,
        31, 31, 30, 31, 30, 31
    };
    char *date = NULL;

    /* allocate storage */
    date = malloc(365*5);
    if( date==NULL )
    {
        fprintf(stderr,"Can't allocate storage\n");
        exit(1);
    }

    /* calculate and store dates */
    offset = 0;
    for( month=0; month<12; month++ )
    {
        for( d=0; d<days[month]; d++ )
        {
            snprintf((date+(offset*5)),5,"%02d%02d",
                    month+1,
                    d+1
                  );
            offset++;
        }
    }

    /* output all dates */
    for(offset=0; offset<365; offset++)
        printf("%s\n",date+(offset*5));

    return 0;
}

Rather than allocate storage on-the-fly (which I coded originally because I’m dumb), this code allocates a huge chunk of memory to store the 365 4-character strings. Variable offset then tracks the depth within the buffer to store the string values. The expression is date+(offset*5), which accesses a position inside the buffer to set the string. This same expression is used in the printf() statement to output the results.

Though coding these two examples was fun, it’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:

2025_11_08-Lesson-c.c

#include <stdio.h>

int main()
{
    int month,d;
    int days[12] = {
        31, 28, 31, 30, 31, 30,
        31, 31, 30, 31, 30, 31
    };
    char date[5];

    for( month=0; month<12; month++ )
    {
        for( d=0; d<days[month]; d++ )
        {
            snprintf(date,5,"%02d%02d",
                    month+1,
                    d+1
                  );
            printf("%s\n",date);
        }
    }

    return 0;
}

Only one, 5-byte storage container is need: date[]. The snprintf() function stores each date in this container, then the date is output.

For next week’s Lesson, I add a function to examine the date to confirm whether it’s a valid binary value.

Leave a Reply