First, I see several C mistakes. Maybe this is what you want:
Code:
#include <stdio.h>
int main(void)
{
static int a=5;
if (--a > 0)
main();
printf("%d", a);
return 0;
}
That function calls itself reentrantly four times while decrementing 'a'. It prints "0" just before it returns each time, so the final output is "00000". Why do you think it should print only "o"?
I've done some modifications which makes it clearer what happens. Main is called 5 times without printing anything until a = 0. Imagine it as a chain. Then the last main is finished by printing a = 0. This happens until all called functions are returned (5 times).
Nice example for a recursive function :roll:
Mik
Code:
void main()
{
static int a=5;
static int b=0;
printf("entering main %d\n", ++b);
if(--a > 0)
main();
printf("%d\n", a);
printf("leaving main %d\n", b--);
}
Like auto variable, static variables are also local to the block in which they are declared. The difference between them is that static variables don't disappear when the function is no longer active. If the control comes back to the same function again the static variables have the same values they had last time around.
HERE UR CALLING THE SAME FUNCTION MAIN FOR 5 TIMES UNTILL THE VARIABLE 'A' IS 0; AT LAST THE VALUE OF 'A' IS 0. THEN IT IS SIMILAR TO FACTORIAL CALCULATION WHERE WHEN U CALL FACT IT WILL MUL USING LAST IN FIRST OUT PRINCIPLE IS USED.
HERE WE ARE CALLING THE FUNCTION FIVE TIMES THUS THE OUTPUT CONTAINS FIVE ZEROS.
my freind you called the main function 5 different time inside it selves;
watch this
1. a os static so it value will be static if you call the fucntiojn again;
1st time you run the program a=4 then call main to the 2nd time
2. in the 2nd main a=3 then call main to the 3rd time
3. in 3rd main a=2 then call main to the 4th time
4. in 4th main a=1 then call main to the 5th time
5. in the 5th main a=0 then program will resume and will not call main again
then print value of a which now is 0
6. return to 4th main to print value of a which is 0 too
7. return to 3rd main to print value of a which is 0 too
8. return to 2nd main to print value of a which is 0 too
9. finally retun to 1st main to print value of a which is 0 too