void main()
{
static int i = 5;
if(--i)
{
main();
printf("%d
",i);
}
}

what would be output of the above program and justify your
answer?

}

Answer Posted / samrat

Ans: 0 0 0 0

The first thing you have to remember is that static
variables are initialized only once. The second thing is
that static variables have a life time scope and they retain
their value between function calls.

"i" is first initialized to 5. in the if condition the value
of i is changed to 4. main() is called again and the value
of i is changed to 3 in the if condition and main is called
again. Now the value of i is changed to 2 and main is called
again. Now the value of i is changed to 1 and main is called
again. After this the value of i is changed to "0" and the
block is excited.

As the value of i is now "0", it is printed 4 times for each
of the calls for main(). So the ans will be
0
0
0
0

Thanks,
Samrat

Is This Answer Correct ?    67 Yes 5 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

I was asked to write a program in c which when executed displays how many no.of clients are connected to the server.

1907


code for quick sort?

1622


Tell us two differences between new () and malloc ()?

612


which is an algorithm for sorting in a growing Lexicographic order

1394


The __________ attribute is used to announce variables based on definitions of columns in a table?

669






Why C language is a procedural language?

618


Should I learn c before c++?

605


What is a sequential access file?

644


What is wrong with this statement? Myname = 'robin';

816


Explain what is a 'locale'?

583


When a c file is executed there are many files that are automatically opened what are they files?

592


Can the size of an array be declared at runtime?

605


What is c value paradox explain?

575


#include main() { enum _tag{ left=10, right, front=100, back}; printf("left is %d, right is %d, front is %d, back is %d",left,right,front,back); }

713


What are the output(s) for the following ? #include char *f() {char *s=malloc(8); strcpy(s,"goodbye")} main() { char *f(); printf("%c",*f()='A'); }

698