What do you think about the following C Code ? What will be the output.

#include <stdio.h>

int main (void)
{
  static int a = 3;

  if (a == 0)
  {
    return 0;
  }

  printf ("going down\n");

  a--;
  main ();

  printf ("coming up\n");

  return 0;
}


It is actually not very difficult to compute the output of this C Code. This has a static integer in the main function a initialized to 3. After the declaration of the variable the value of the variable is checked if it is zero. If yes the control returns, else it prints the string “Going Down”, decrements the value of a and then calls main (recursively). Because a is static it will remember the updated value throughout the subsequent function calls (lifetime throughout the execution of the code). As a is initialized to 3, after 3 such recursive calls a will be 0 and the the if block will be executed, which will start the roll back of the recursive call. After returning to each level of the recursive depth of the function the string “Coming Up” will be printed (total 3 times), and then the program terminates.

There is no magic in the program, and basically i have wasted 2 mins writing the above paragraph. This is not the target of this post, to show that it is possible to make recursive calls in main. Instead the target of the post is to show how things differ in the case of C++.

You probably have noted that i have emphasized that the code is in C language like this: “C Code”.

What do you think the output will be if the same code was written in C++ (replacing the printfs with cout). Here is the modified code (for the sake of completeness).

#include <iostream>

using namespace std;

int main (void)
{
  static int a = 3;

  if (a == 0)
  {
    return 0;
  }

  cout << "going down\n";

  a--;
  main ();

  cout << "coming up\n";

  return 0;
}

If you compile the above piece of code it will compile without any errors or warnings (Tested in g++ (GCC) 4.6.3 20120306 (Red Hat 4.6.3-2)). Also it will (may) run and have identical output as it was in C. So what’s the point?

The point is that calling the main function inside the program and also calling it recursively is undefined behaviour. C++ Standards talk about these in

Section 3.6.1 Paragraph 3

Recursive calls are permitted, except to the function named main.

and Section 5.2.2 Paragraph 9

The function main shall not be used within the program. … … …

If you compile with the -pedantic flag in g++ then you can see the compiler talking about this issue.

$ g++ rec_main.cpp -pedantic
rec_main.cpp: In function ‘int main()’:
rec_main.cpp:15:9: warning: ISO C++ forbids taking address of function ‘::main’ [-pedantic]

I did not find any such restriction in the C standards. What i found about recursive calls in the C99 standards in Section 6.5.2.2 Paragraph 11 is as follows

Recursive function calls shall be permitted, both directly and indirectly through any chain of other functions.

Therefore calling main recursively in C Language is deterministic. But as per C++ standards calling main from any function or recursively is not allowed.

So be careful, C++ is not an updated and advanced C Language, it is a different language, which look like C.

3 thoughts on “C Q&A #4: Calling main recursively

  1. Ohh…. extremely sorry… the post was not completely loaded. Some of the sections in the middle were not seen at the first time. Now I can understand.

Leave a comment