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);
}- A.
20 10 10
- B.
20 10 20
- C.
20 20 20
- D.
10 10 10
Attempted by 436 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.