in Programming in C edited by
1,014 views
1 vote
1 vote
void ab()
{
    auto int a;
    static int s= 5;
    a = ++s;
    printf("%d%d",a,s);
    if(a<= 7)
    ab();
    printf("%d%d",a,s);
}
void main()
{
    ab();
}



According to me answer should be- 667788887766 but the answer is - 667788887868. Please explain

in Programming in C edited by
1.0k views

2 Comments

static variable store in static part of memory and once changed then retain their value throughout the program.

So S will print 8 only after ending of each activation record.
0
0

667788887868 is correct answer. 

since, s is static & static variables are stored data area of memory, which is not destroyed. So, finally updated value of 's' (which is 8) will be printed, while coming back recursively.

However, a is auto so, it is stored in stack. so, each time local stack values will be taken for 'a' & stack is destroyed, while coming back recursively.

0
0

1 Answer

2 votes
2 votes
Best answer

Ans-667788887868

a is local variable and s is static variable so scope of local variable is local and it will destroy after function is over and scope of static variable is throughout the program.

selected by

4 Comments

thanks @Anil Ji
0
0
Anil ji you are so great
0
0
I did not get it. in last how 887868 is printed? when $8\leq7$ this condition is false then what happens?
0
0

Related questions