{"id":7661,"date":"2026-07-04T00:01:58","date_gmt":"2026-07-04T07:01:58","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7661"},"modified":"2026-06-28T12:58:37","modified_gmt":"2026-06-28T19:58:37","slug":"happy-250th-usa","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7661","title":{"rendered":"Happy 250th, USA!"},"content":{"rendered":"<p><a href=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/06\/0704_figure1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/06\/0704_figure1-300x141.png\" alt=\"US Flag in ASCII, color characters, red, white, and blue\" width=\"300\" height=\"141\" class=\"alignnone size-medium wp-image-7662\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/06\/0704_figure1-300x141.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/06\/0704_figure1-768x361.png 768w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/06\/0704_figure1-500x235.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2026\/06\/0704_figure1.png 912w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><br \/>\nToday is Independence Day in the USA. Two hundred and fifty years ago, this nation&#8217;s founding fathers signed the Declaration of Independence. While I&#8217;ve had posts dated July 4 previously on this blog, today is a special anniversary. It necessitates a specific C language program.<br \/>\n<!--more--><br \/>\nTo output an American flag, as shown above, I used standard ASCII characters, spaces and asterisks. I added <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5270\">ANSI color codes<\/a> 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 <em>printf(<\/em>) statements that output each stripe on the flag, but I didn&#8217;t.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_07_04-Lesson.c\" rel=\"noopener\" target=\"_blank\">2026_07_04-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define RED \"\\e[41m\"\r\n#define WHITE \"\\e[47m\"\r\n#define BLUE \"\\e[37;44m\"\r\n#define NORMAL \"\\e[m\"\r\n#define LONG 35\r\n#define SHORT 22\r\n\r\nvoid bar(char *color,int size)\r\n{\r\n    printf(\"%s\",color);\r\n\r\n    while(size--)\r\n        putchar(' ');\r\n    printf(\"%s\\n\",NORMAL);\r\n}\r\n\r\nint main()\r\n{\r\n    int star,stripe,count;\r\n\r\n    for( stripe=0,star=0; stripe&lt;13; stripe++ )\r\n    {\r\n        printf(\"%s\",BLUE);\r\n        if( stripe&lt;7 )\r\n        {\r\n            for(count=0;count&lt;SHORT;count++,star++)\r\n                !(star%4) ? putchar('*') : putchar(' ');\r\n            stripe%2 ? bar(WHITE,LONG) : bar(RED,LONG);\r\n        }\r\n        else\r\n        {\r\n            stripe%2 ? bar(WHITE,LONG+SHORT) : bar(RED,LONG+SHORT);\r\n        }\r\n    }\r\n\r\n    return 0;\r\n}<\/pre>\n<p>As series of <em>define<\/em> directives configure the ANSI codes for colors <code>RED<\/code>, <code>WHITE<\/code>, <code>BLUE<\/code>, and <code>NORMAL<\/code>. Defined constants <code>LONG<\/code> and <code>SHORT<\/code> set the length of each stripe; the <code>SHORT<\/code> values help define the blue field.<\/p>\n<p>Function <em>bar()<\/em> outputs one of the 13 bars in a specific color and for a specific length (argument <code>size<\/code>).<\/p>\n<p>The <em>main(<\/em>) function outputs the entire flag, using variable <code>stripe<\/code> to set the number of stripes (13) in a <em>for<\/em> loop. When the value of <code>stripe<\/code> is less than 7, the blue star field is output. A nested <code>for<\/code> loop outputs the blue field, using variable <code>star<\/code> to determine when a star (asterisk) appears. It&#8217;s in this loop where you see this contraption:<\/p>\n<p><code>!(star%4) ? putchar('*') : putchar(' ');<\/code><\/p>\n<p>Variable <code>star<\/code> isn&#8217;t a looping variable. It increments continuously as the inner <em>for<\/em> loop spins. For every value of <code>star<\/code> 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.<\/p>\n<p>The stripe color is set by testing the value of variable <code>stripe<\/code> odd or even:<\/p>\n<p><code>stripe%2 ? bar(WHITE,LONG+SHORT) : bar(RED,LONG+SHORT);<\/code><\/p>\n<p>The program&#8217;s output appaars at the top of this post.<\/p>\n<p>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.<\/p>\n<p>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&#8217;s capabilities (or my coding skills).<\/p>\n<p>Happy 250<sup>th<\/sup>, America!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s time for America to celebrate a great birthday. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7661\">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-7661","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\/7661","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=7661"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7661\/revisions"}],"predecessor-version":[{"id":7672,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7661\/revisions\/7672"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7661"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7661"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7661"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}