edited by
713 views
1 votes
1 votes

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$
edited by

3 Answers

1 votes
1 votes
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 votes
1 votes
b iscorrect anser

here is post decrement opertion performed on a : so , print 3,2,1
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

3 votes
3 votes
1 answer
1
admin asked Apr 1, 2020
975 views
The following program fragment printsint i = 5; do { putchar(i+100); printf(“%d”, i ;) } while(i);i5h4g3f2el14h3g2f1e0An error messageNone of the above
0 votes
0 votes
0 answers
3
admin asked Aug 28, 2020
306 views
In MOSFET fabrication, the channel; length is defined during the process ofIsolation oxide growthChannel stop implantationPoly-silicon gate patterningLithography step lea...
4 votes
4 votes
4 answers
4
admin asked Apr 1, 2020
1,297 views
Synthesized attribute can easily be simulated by anLL grammarambiguous grammarLR grammarnone of the above