-3

Why does it give an error of segmentation fault(core dumped) and gives no output in 1st case ?Can anyone explain how program is program callls main recursively with parameters?

#include <stdio.h>
int main()
{
    static int i = 2;
    if (i<7)
    {
        main();
        printf("%d ", i);
        i++;
        //main(10);
    }
}

-

#include <stdio.h>
int main()
{
    static int i = 5;
    if (--i)
    {
     //main();
     printf("%d ", i);
     main(10);
    }
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
ash
  • 39
  • 8

6 Answers6

3

You are calling main() first and then incrementing i

 if (i<7)
    {
        main();           // <-- main called when i still 2
        printf("%d ", i);
        i++;              // <-- statement never reached
        //main(10);
    }

Hence, while main() calls main() recursively, the statement i++ is never reached.

You should increment i before calling main()

if (i<7)
    {
        i++;
        main();
        printf("%d ", i);
        //main(10);
    }
Haris
  • 12,120
  • 6
  • 43
  • 70
1

TL;DR StackOverflow (Pun or no pun, it's true both ways).

First: Some important information

This case has nothing to do with passing an argument to main() or not. Actually, as per the latest C standard, C11, for a hosted environment, the conforming signature of main() is int main(void) and following that, main(10); is wrong, altogether.


Now, coming to the actual problem here,

  • In the first case, change of i value happens after the call to main(), so in effect, the value of i never get changed because the control never reaches the statement which modifies i. Hence, it's an infinite loop, and because of the recursive call to main(), stack overflow happens.

  • In later case, i value gets updated before call to main(), so that value actually reflects. Thus, at some point (after 4 occurrences, actually), the if condition meets a FALSE condition and the recursive call ends and the stack unwinds.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • @ash Well, that is explained in the answer. You can just call `main()` in the second call also, will not be a problem. – Sourav Ghosh Jun 08 '17 at 08:10
1

1st case the i++ after the call of main(), that means the program has no chance to add the i, and into infinity loop, and stackoverflow! haha....

but the 2nd case, it's reduce the i before you call the main.

Jimmy
  • 223
  • 1
  • 11
1

First case:

i++ is positionned after the call to main. So i will always be equal to 2, as you never reach this part of the code. You are then calling your main function everytime, leading to an infinite recursion, and thus to the segmentation fault. The fix for this case would be to increase i before calling the main function.

if (i<7)
{
    i++; // Increment before calling the main function, so i value is changed
    main();
    printf("%d ", i);
}

Do note that it will lead to something which looks like your second case, except that you will print several "7".

Seconde case:

In the second case, you are decreasing the value of i everytime you enter your if condition. When you finally can't enter your if condition anymore, it means i is equal to 0 (as if(0) is equivalent to if(false)). Everytime you will return to the previous called main function, i will still be equal to 0, explaining why you are displaying "0" everytime. If you want to print the different values, you can place your printf before the call to the main function for instance.

Izuka
  • 2,572
  • 5
  • 29
  • 34
0

In the first case:

"i" is never incremented therefore you run in an infinite loop and get an overflow.

In the second case:

You call the recursion before displaying the value of "i", after four calls "i" equals to 0 and your recursion stack unwinds.

Modify your condition and perform outputs before the recursion call.

true_gler
  • 544
  • 6
  • 23
0

In the first case, i is never incremented, hence, the main keeps getting called and soon enough there is no more stack memory available to the program.

In the second case, the last updated value of i is intact throughout the lifetime of i. As i is static, program is referring to the same memory address on all the iterations. While printing i you see the last updated value, that is 0, as output.

awakened
  • 192
  • 9