1 votes 1 votes 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 Programming in C recursion programming-in-c + – parasghai28 asked Jul 8, 2018 • edited Dec 27, 2023 by Hira Thakur parasghai28 1.1k views answer comment Share Follow See all 2 Comments See all 2 2 Comments reply Shubhgupta commented Jul 8, 2018 reply Follow Share 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 votes 0 votes Naveen Kumar 3 commented Jul 8, 2018 reply Follow Share 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 votes 0 votes Please log in or register to add a comment.
Best answer 2 votes 2 votes 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. Anil Ji answered Jul 8, 2018 • selected Jul 8, 2018 by parasghai28 Anil Ji comment Share Follow See all 6 Comments See all 6 6 Comments reply altamash commented Jul 8, 2018 reply Follow Share can u explain again not getting 0 votes 0 votes Anil Ji commented Jul 8, 2018 reply Follow Share In the main ab() is called ,when ab() is called ,as in ab() a is local variable(lifetime and scope is local) and s is static variable and here s is assigned value 5 ,then a=++s so a will have value 6 and s will have value equal to 6 so 66 will be printed then condition will be check a<=7 (6<=7 true) so ab() will be called again without reaching the last line ,now a will be created again and s will not be created as it is static and a=++s will executed and now a and s will have 7 and 7 so 77 will be printed now again condition will be checked a<=7(7<=7 true) ,now ab() will be called one more time ,once again a will be created and a=++s will be executed so a and s will have value 8 and 8 so 88 will be printed ,then condition will be checked a<=7(8<=7 false) so execution will go to the last line ,now 887868 will be printed as i described in image so finally 667788887868 will be printed. 0 votes 0 votes parasghai28 commented Jul 8, 2018 reply Follow Share Thanks :) 0 votes 0 votes altamash commented Jul 9, 2018 reply Follow Share thanks @Anil Ji 0 votes 0 votes Rajucse commented Jul 16, 2018 reply Follow Share Anil ji you are so great 0 votes 0 votes Hira Thakur commented Dec 27, 2023 reply Follow Share I did not get it. in last how 887868 is printed? when $8\leq7$ this condition is false then what happens? 0 votes 0 votes Please log in or register to add a comment.