Saturday, January 2, 2010

Lpart1

#include< stdio.h >
void main()
{
static int a=4;
printf("%d",a--);
if(a)
main();
}
what'll be the answer?


  1. run continuously

  2. compile time error

  3. print 3 2 1 0

  4. print 4 3 2 1












For Static variable, memory is statically allocated .
At first a is declared and initialize to 4 .
a=4 is printed and then a is decreament to 3.
Then if(a) will work main will called itself . But note that now a is 3. Then it'll print 3 and a is decreament to 2. Like that it 'll print 4 3 2 1. When a=0 then if(a) is false . So it won't call main function.

0 comments:

Post a Comment