The challenge for this month’s Exercise is to code a program that loops n times without using a looping keyword or statement. As you can probably guess, the solution involves recursion.
For my approach, I created the recursive function count(). It has two arguments, n and a. Variable n is the number to count to with variable a keeping track of the iterations. The function handles the output and has no return value:
2026_11-Exercise.c
#include <stdio.h>
void count(int n,int a)
{
printf("%d\n",a);
if( a==n )
return;
else
count(n,a+1);
}
int main()
{
int n;
/* obtain input */
printf("Loop count = ");
scanf("%d",&n);
/* test for bad input */
if( n<1 )
{
puts("Enter a value larger than zero");
return -1;
}
/* start at zero */
count(n,1);
return 0;
}
The main() function handles input. Only positive integers are accepted. The value input is passed to the count() function with a second argument of one as the starting point. Here’s a sample run:
Loop count = 10 1 2 3 4 5 6 7 8 9 10
The recursion in the count() function works by having the function call itself as long as arguments a and n are unequal. Along the way, the value of variable a is output:
printf("%d\n",a);
With the values unequal, the recursive call is made by adding one to the value of a:
count(n,a+1);
Eventually, n and a will equal with each other, at which point the return statement is called and the recursion unwinds.
I’ll confess that some of the more interesting solutions to this puzzle are possible only in C++. I am a total noob when it comes to C++, though here is a solution I found on the interwebs:
2026_11-1108.cpp
#include <iostream>
using namespace std;
#define N 10
int main()
{
static int i = 1;
if( i <= N && cout << i++ << "\n" ) {
main();
}
return 0;
}
The output mirrors my solution, but without the prompt for input. This solution also uses recursion, but with the main() function — which I hear is scary. Regardless, it works.
Do yourself a favour and ignore C++. It’s a mess, although better than what C# has evolved into.
I experimented with a one-line solution using goto (despite your prohibition!) and a ternary but it didn’t compile.
I first solved the given task using recursion:
void count_recursively (unsigned long long number)
{
if (number > 1)
count_recursively (number-1);
fprintf (stdout, "%llu\n", number);
}
However, I found that this limits ‘number’ to about 174 thousand recursive calls, if the compiler in question doesnʼt perform a tail-call optimization.
So I next thought up two helper functions:
• store_address_of_next_statement (…)
Inspects the functions stack frame and returns the stored return address.
• return_to_loop_start_addr (void)
Overwrites the return address on the stack to jump to a specific location in memory.
With these two helper functions I managed to write a function count_via_return() that performs a kind of manually hacked together goto… without actually writing goto:
static void count_via_return (unsigned long long number)
{ static thread_local unsigned long long current_counter = 1;
store_address_of_next_statement (&loop_start_addr);
/*loop_start:*/
fprintf (stdout, "%llu\n", current_counter);
current_counter++;
if (current_counter > number)
{
current_counter = 1;
return;
}
return_to_loop_start_addr ();
}
The program works under Linux and Windows and is based on the idea that the return address of a function can be determined under GCC by evaluating the expression (char *)__builtin_frame_address(0)+sizeof(void *) and under Windows by invoking _AddressOfReturnAddress();
All the solutions I found on the interwebs used recursion. Some used specific language tricks and some tricks unique to exploits in the compiler. But, no: It’s recursion.
I had an opportunity recently to update a C++ book. Passed.