{"id":7695,"date":"2026-08-01T00:01:53","date_gmt":"2026-08-01T07:01:53","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7695"},"modified":"2026-07-25T10:44:54","modified_gmt":"2026-07-25T17:44:54","slug":"how-much-stuff-can-you-cram-in-there","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7695","title":{"rendered":"How Much Stuff Can You Cram In There?"},"content":{"rendered":"<p>The <em>for<\/em> keyword opens the door to looping in C, as well as in other programming languages. When teaching this concept, I keep the <em>for<\/em> loop format simple. After all there&#8217;s a lot of cryptic stuff between those parenthesis. And \u2014 brace yourself \u2014 there&#8217;s room for a lot of other stuff as well.<br \/>\n<!--more--><br \/>\nFirst comes the basic format, as presented in my C programming books:<\/p>\n<p><code>for( <em>initialization<\/em>; <em>exit_condition<\/em>; <em>repeat_each<\/em> )<\/code><\/p>\n<p>Each of the items in the parentheses are separated by semicolons. These semicolons are the only required parts of the statement; you can omit any of the other items but the semicolons must remain. In fact, eliminating everything results in an endless loop:<\/p>\n<p><code>for(;;)<\/code><\/p>\n<p>I pronounce this statement as &#8220;<em>for<\/em>-ever.&#8221;<\/p>\n<p>As a statement, the <em>for<\/em> loop ends with a semicolon or it can be followed by a block of statements enclosed in braces. Blah, blah, blah, you should know the rest if you program in C. If you&#8217;re just learning, <a href=\"https:\/\/amzn.to\/3OwfNo4\" target=\"_blank\" rel=\"noopener\">buy my book<\/a>.<\/p>\n<p>Second, once you become comfortable with the format you can shove other things into a <em>for<\/em> statement. Each item goes in its own part of the statement and is acted upon in the same manner, though multiple items are separated by commas. The commas add to the delightful confusion of the contraption:<\/p>\n<p><code>for( x=0, y=10; x&lt;10; x++,y-- )<\/code><\/p>\n<p>In this loop, both variables <code>x<\/code> and <code>y<\/code> are initialized: <code>x<\/code> to zero and <code>y<\/code> to ten. Each is separated by a comma. This initialization takes place first.<\/p>\n<p>The loop continues as long as the value of variable <code>x<\/code> is less than ten.<\/p>\n<p>For each iteration, the value of variable <code>x<\/code> is incremented and the value of variable <code>y<\/code> is decremented. Again, each expression is separated by a comma, but both operations take place each time the loop spins.<\/p>\n<p>So what else can you cram in there?<\/p>\n<p>The <em>for<\/em> keyword description defines each of its three items as expressions. In C, an expression is evaluated TRUE or FALSE. Assignments are evaluated TRUE. Just about anything in C can be an expression, even a function as the following code demonstrates:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2026_08_01-Lesson.c\" target=\"_blank\" rel=\"noopener\">2026_08_01-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">#include &lt;stdio.h&gt;\r\n\r\nvoid scale(void)\r\n{\r\n    char s;\r\n\r\n    for( s='A'; s&lt;='G'; putchar(s),s++ )\r\n        ;\r\n}\r\n\r\nint main()\r\n{\r\n    int x;\r\n\r\n    for( x=0; x&lt;10; scale(), printf(\"%d loop\\n\",x) ,x++ )\r\n        ;\r\n\r\n    return 0;\r\n}<\/pre>\n<p>The <em>for<\/em> loop in the <em>main()<\/em> function executes three expressions for each iteration: The <em>scale()<\/em> function is called, a <em>printf()<\/em> statement outputs text, and the value of variable <code>x<\/code> is incremented.<\/p>\n<p>Within the <em>scale()<\/em> function, the <em>putchar()<\/em> function is set inside the <em>for<\/em> loop.<\/p>\n<p>Here&#8217;s the program&#8217;s output:<\/p>\n<pre>ABCDEFG0 loop\r\nABCDEFG1 loop\r\nABCDEFG2 loop\r\nABCDEFG3 loop\r\nABCDEFG4 loop\r\nABCDEFG5 loop\r\nABCDEFG6 loop\r\nABCDEFG7 loop\r\nABCDEFG8 loop\r\nABCDEFG9 loop<\/pre>\n<p>The point of this lesson was to see whether I could stick all statements in a program inside a <em>for<\/em> loop. What is the limit?<\/p>\n<p>In the sample code, you see the <em>scale(<\/em>) function. This function is required because one of the things you can&#8217;t stick inside a <em>for<\/em> loop statement is another <em>for<\/em> loop; <em>for<\/em> is a keyword and its operation isn&#8217;t evaluated as an expression. So, you cannot stick a <em>for<\/em> loop inside a <em>for<\/em> loop statement, but you can stick a function in there and the function can have a <em>for<\/em> loop, which is how I worked around this limitation.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The <em>for<\/em> statement can be overloaded with lotsa stuff. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7695\">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-7695","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\/7695","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=7695"}],"version-history":[{"count":4,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7695\/revisions"}],"predecessor-version":[{"id":7709,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7695\/revisions\/7709"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7695"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7695"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7695"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}