52

int main()
{
static int a=4;
if(a!=0)
{
--a;
main();
printf("%d",a);
}
return 0;
}

Output


0000


Logic:
"static variables retain their values even after the function terminates.
as a result if a function terminates and then is re-entered later,static variables retain their former values."


21-4.jpg
until the 'a' becomes zero the main will be called .
when the condition if(a!=0) is false i.e,a=0, the main function come to end and returned to previous main function.
in previous main function printf("%d",a); will work so 0 will be printed(static variable so a remains zero).such that all main will be return like this

main->main->main->main->main
0 0 0 0 a values

Related Posts