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.