Happy 250th, USA!

US Flag in ASCII, color characters, red, white, and blue
Today is Independence Day in the USA. Two hundred and fifty years ago, this nation’s founding fathers signed the Declaration of Independence. While I’ve had posts dated July 4 previously on this blog, today is a special anniversary. It necessitates a specific C language program.

To output an American flag, as shown above, I used standard ASCII characters, spaces and asterisks. I added ANSI color codes to create the red, white, and blue text backgrounds. So far, so good. In fact, I could have just written the code as a series of printf() statements that output each stripe on the flag, but I didn’t.

Before going further: Yes, I know that the US flag features 50 stars in its blue field. These stars are not aligned with the red and white stripes. So, when my code renders the blue field, it outputs only 39 stars. I hereby apologize to the following states: South Dakota, Montana, Washington, Idaho, Wyoming, Utah, Oklahoma, New Mexico, Arizona, Alaska, Hawaii.

As for the code to output the US flag, I decided to get fancy. The red and white stripes alternate, as do the stars in the blue field. I built my code to optimize the output. The result is a bit obfuscated but not heinously so.

2026_07_04-Lesson.c

#include <stdio.h>

#define RED "\e[41m"
#define WHITE "\e[47m"
#define BLUE "\e[37;44m"
#define NORMAL "\e[m"
#define LONG 35
#define SHORT 22

void bar(char *color,int size)
{
    printf("%s",color);

    while(size--)
        putchar(' ');
    printf("%s\n",NORMAL);
}

int main()
{
    int star,stripe,count;

    for( stripe=0,star=0; stripe<13; stripe++ )
    {
        printf("%s",BLUE);
        if( stripe<7 )
        {
            for(count=0;count<SHORT;count++,star++)
                !(star%4) ? putchar('*') : putchar(' ');
            stripe%2 ? bar(WHITE,LONG) : bar(RED,LONG);
        }
        else
        {
            stripe%2 ? bar(WHITE,LONG+SHORT) : bar(RED,LONG+SHORT);
        }
    }

    return 0;
}

As series of define directives configure the ANSI codes for colors RED, WHITE, BLUE, and NORMAL. Defined constants LONG and SHORT set the length of each stripe; the SHORT values help define the blue field.

Function bar() outputs one of the 13 bars in a specific color and for a specific length (argument size).

The main() function outputs the entire flag, using variable stripe to set the number of stripes (13) in a for loop. When the value of stripe is less than 7, the blue star field is output. A nested for loop outputs the blue field, using variable star to determine when a star (asterisk) appears. It’s in this loop where you see this contraption:

!(star%4) ? putchar('*') : putchar(' ');

Variable star isn’t a looping variable. It increments continuously as the inner for loop spins. For every value of star divisible by four, an asterisk is output. This pattern continues for the entire blue field, which is how each row is offset within the output.

The stripe color is set by testing the value of variable stripe odd or even:

stripe%2 ? bar(WHITE,LONG+SHORT) : bar(RED,LONG+SHORT);

The program’s output appaars at the top of this post.

As you might guess, my first version of this code was more readable. I had to plot out the entire flag just as I wanted before I figured out how to code the blue field and then how to plant the stars in a repeatable pattern.

Jamming the extra 11 stars into the blue field to represent all 50 US states would require setting the asterisks at specific screen locations. Positioning characters outside the standard character grid is beyond the terminal’s capabilities (or my coding skills).

Happy 250th, America!

2 thoughts on “Happy 250th, USA!

  1. If you make the blue area wider you could fit in 50 stars but it would need two consecutive columns of four – a column of three and two columns of four.

    It would be pretty easy to write an SVG version so you could have the exact positions. Basically just a load of rectangles and a separate function that creates a path for a star at the specified coordinates, called within a loop.

    Apparently it’s possible to make a star from a square of fabric folded twice and cut once.

    “I could have just written the code as a series of printf() statements that output each stripe on the flag” That would be the Instagram method. If anyone new to programming is reading this, don’t use Instagram as a learning resource!

Leave a Reply