{"id":6902,"date":"2025-04-08T00:01:25","date_gmt":"2025-04-08T07:01:25","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6902"},"modified":"2025-04-05T10:50:54","modified_gmt":"2025-04-05T17:50:54","slug":"a-nutty-non-loop-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6902","title":{"rendered":"A Nutty Non-Loop &#8211; Solution"},"content":{"rendered":"<p>The insane challenge for <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6886\">this month&#8217;s Exercise<\/a> is to generate a countdown loop without using any looping functions. And, no, you can&#8217;t just use a buncha <em>printf()<\/em> statements to output the values.<br \/>\n<!--more--><br \/>\nIf you&#8217;ve been reading this blog for any length of time, you probably know that when I use the word &#8220;insane&#8221; I&#8217;m referring to recursion. The inspiration for the exercise is based on the possibility of recursively calling the <em>main()<\/em> function.<\/p>\n<p>I demonstrated a single recursive call to the <em>main()<\/em> function in a post from several years ago (<a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3187\">Stupid <em>main()<\/em> Function Tricks<\/a>). Upon reviewing that old post, I thought about taking the insanity a step further and using recursion in <em>main()<\/em> to output descending values. Here is the result, and my solution:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_04-Exercise.c\" rel=\"noopener\" target=\"_blank\">2025_04-Exercise.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main(int argc, char *argv[])\r\n{\r\n    if( argc==11 )\r\n        return argc;\r\n    printf(\"%d\\n\",main(argc+1,NULL)-1);\r\n\r\n    return argc;\r\n}<\/pre>\n<p>The <em>main()<\/em> function calls itself within the <em>printf()<\/em> statement. Its arguments are the values of <code>argc<\/code> (the argument count) plus one and NULL. The NULL is required and valid, but unusable. It doesn&#8217;t matter, however, as the countdown is what&#8217;s stored in <code>argc<\/code> and what&#8217;s eventually output.<\/p>\n<p>The trick is to get the values to output in reverse order. Therefore, the exit trigger for recursion is <code>argc==11<\/code> &mdash; one more than ten. Once triggered, the value of <code>argc<\/code> is returned and output in the <em>printf()<\/em> statement.<\/p>\n<p>As recursion unwinds, each subsequent <code>argc<\/code> value is output, 9 through 1. The final <code>return argc<\/code> statement sends the last value, 1, to the operating system.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>10<br \/>\n9<br \/>\n8<br \/>\n7<br \/>\n6<br \/>\n5<br \/>\n4<br \/>\n3<br \/>\n2<br \/>\n1<\/code><\/p>\n<p>I hope that your solution also met with success. And if you came up with something different, I&#8217;d enjoy seeing it.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The insane challenge for this month&#8217;s Exercise is to generate a countdown loop without using any looping functions. And, no, you can&#8217;t just use a buncha printf() statements to output the values.<\/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-6902","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\/6902","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=6902"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6902\/revisions"}],"predecessor-version":[{"id":6925,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6902\/revisions\/6925"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6902"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6902"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6902"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}