-3
#include <stdio.h>

int main()
{
    static int i = 5;

    if (--i)
    {
        main();
        printf("%d\n", i); // will this line executes ?
    }

    return 0;
} 

Output:

0
0
0
0

does code below main(); printf statement instructions is placed to stack every time when main recursive calls happens and executed while terminated from this program?

  • 2
    This is basic recursion. I suggest you read up on it as well as how `static` works. – Spikatrix Jul 16 '18 at 09:13
  • 1
    Welcome to SO. Please always show your current effort and tell us where **exactly** you have problems. If the output is not what you expect, tell us what you expect. – Gerhardh Jul 16 '18 at 09:18
  • 3
    @DevSolar the site is supposed to build a searchable knowledge base that other people can use as a reference site ... there is no way that a question titled "Can someone explain the output of this program" with no further text besides the code, will ever help anybody else – M.M Jul 16 '18 at 09:46
  • 1
    @M.M: This site is also supposed to help users with their problems; do we downvote questions because giving them a better title would have required knowledge that the OP *does not have* about the problem? If he'd known all about `static` and recursion, *he would not have had to ask*. – DevSolar Jul 16 '18 at 09:51
  • See [Why is "can someone help me?" not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question/303544#303544) – M.M Jul 16 '18 at 10:01
  • 2
    In fact, the question has been asked for exacly the same program at least 5 times already. I closed it as a duplicate to the oldest one that I found. – Jens Gustedt Jul 16 '18 at 10:17

3 Answers3

5

i is reduced by successive calls to main until zero is reached.

Then printf is called for each level of recursion.

(Note that the behaviour of calling main from itself is well-defined although ill-advised in C, in C++ the behaviour is undefined.)

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • Calling `main()` is not undefined in C++. Its usage is forbidden. – Peter Jul 16 '18 at 09:18
  • @Peter: U wot? A compiler does not have to issue a diagnostic. – Bathsheba Jul 16 '18 at 09:20
  • 1
    @Bathesba - the actual wording in the 1998 C++ standard is "The function `main()` shall not be used (3.2) within a program". In section 3.2, a function or object is said to be *used* if its name appears in a potentially evaluated expression. There is no reference in any of that discussion to "no diagnostic required" or to undefined behaviour - which means (Section 1.4) that it is a diagnosable rule. – Peter Jul 16 '18 at 09:33
  • 1
    @peter I think you should ask a question on the c++ tag on this. – Bathsheba Jul 16 '18 at 09:34
5
if (--i)

This will evaluate true the first time (--i == 4). The code recurses into main(). (Recursion: A function calling itself.)

As i is static, it will retain its value of 4 (as opposed to an automatic variable, which would be initialized to 5 again). The if (--i) in this second execution of main() will again be true (evaluating to 3), and will again call main() (for a third execution of the function).

The same for --i == 2 and --i == 1, for four executions of main() (including the first, non-recursive one) total that are evaluating the if condition to true.

The next recursion will evaluate the if condition to --i == 0, and thus false. Skipping the if clause, the function call will just return. i is zero at this point, and -- being static, i.e. only one persistent i for all instances of main() -- will remain at that value.

The main() call one level up the stack -- the one that evaluated --i == 1, then called main() and was waiting for it to return -- will now continue with the statement after the call to main(), and printf() the current value of i... which is 0.

The same happens three more times (for a total of four) until the top-most main() returns. You get four times the current value of i, which is 0.


Note that calling main() from your program is allowed in C, but not in C++. This is specifically for calling main() recursively; for other functions, it is allowed in either language.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
-2

(--i) will decrease the value of i to 4 when it is called for the first time and it will continue the decrement of i until (i==0) due to recursion. Since you have declared the variable i with static keyword and hence a single memory is allocated to it and all the changes are reflected back to it

Harsh
  • 70
  • 7