in Programming in C edited by
698 views
1 vote
1 vote

What will be the output of following?

main()

{

         Static int a = 3;

         Printf(“%d”,a--);

         If(a)

         main();

}
  1. $3$
  2. $3\;2\;1$
  3. $3\;3\;3$
  4. Program will fall in continuous loop and print $3$
in Programming in C edited by
by
698 views

1 comment

i think B is right

what is answer ???????
0
0

3 Answers

1 vote
1 vote
Since static variable maintains initial value once assigned ,it will not change within tje scope.

Hence first it will print 3, then on decrementstion it will print 2, and so on 1.

Hence it will print 3 2 1

Hence the option B is correct.
1 vote
1 vote
b iscorrect anser

here is post decrement opertion performed on a : so , print 3,2,1
by
0 votes
0 votes

Definition of static variables: "Static variables have a property of preserving their value even after they are out of their scope. Hence, static variables preserve their previous value in their previous scope and are not initialized again in the new scope."

Reference: Static variables in C

Now that $a$ is a static variable, execution goes as follows:

$3$  is printed and then $a$  is decremented to $2$ (post-decrement operation)

In the first recursive call,  $2$  is printed and $a$  is decremented to 1

In the second recursive call,  $1$  is printed and $a$ is decremented to $0$.

Now, the  $if$  block won't execute as  $a=0$(not true value)

So, the Output printed would look like:   $321$

Option B is correct.

-----------------------------------------------------------------------------------------------------------------------------------

The program output is shown below by executing it on GNU GCC version 5.4.0:

 

 

Answer:

Related questions