
Immediately is Independence Day within the USA. 200 and fifty years in the past, this nation’s founding fathers signed the Declaration of Independence. Whereas I’ve had posts dated July 4 beforehand on this weblog, immediately is a particular anniversary. It necessitates a particular C language program.
To output an American flag, as proven above, I used customary ASCII characters, areas and asterisks. I added ANSI shade codes to create the pink, white, and blue textual content backgrounds. To this point, so good. Actually, I might have simply written the code as a collection of printf() statements that output every stripe on the flag, however I didn’t.
Earlier than going additional: Sure, I do know that the US flag options 50 stars in its blue area. These stars are usually not aligned with the pink and white stripes. So, when my code renders the blue area, it outputs solely 39 stars. I hereby apologize to the next states: South Dakota, Montana, Washington, Idaho, Wyoming, Utah, Oklahoma, New Mexico, Arizona, Alaska, Hawaii.
As for the code to output the US flag, I made a decision to get fancy. The pink and white stripes alternate, as do the celebrities within the blue area. I constructed my code to optimize the output. The result’s a bit obfuscated however not heinously so.
2026_07_04-Lesson.c
#embody <stdio.h>
#outline 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("%sn",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!

