What is the output of the following program? #include int tmp=20; main( ) {…

2017

What is the output of the following program? 

#include 
int tmp=20;
main( )
{
printf("%d ",tmp);
func( );
printf("%d ",tmp);
}
func( )
{
static int tmp=10;
printf("%d ",tmp);
}

Answer: B. 20 10 20The output sequence is 20 10 20. The global variable tmp (initialized to 20) prints first in main. Inside func(), a static local variable tmp (initialized to…

  1. A.

    20 10 10

  2. B.

    20 10 20

  3. C.

    20 20 20

  4. D.

    10 10 10

Attempted by 741 students.

Show answer & explanation

Correct answer: B

The output sequence is 20 10 20. The global variable tmp (initialized to 20) prints first in main. Inside func(), a static local variable tmp (initialized to 10) shadows the global one and prints 10. Control returns to main, printing the unchanged global tmp (20) again.

Explore the full course: Iocl Engineers Officers Grade A Paper 2

Loading lesson…