Skip to content

ISRO Scientist/Engineer CSE 2017Programming in C › Program Output and Loops

ISRO Scientist/Engineer CSE 2017 PYQ: What is the output of the following program? #include<stdio.h> int tmp=20; main() { printf("%d", tmp);…

What is the output of the following program?

#include<stdio.h>
int tmp=20;
main()
{
  printf("%d", tmp);
  func();
  printf("%d", tmp);

}
func()
{
 static int tmp=10;
 printf("%d", tmp);
}
  1. A
    20 10 10
  2. B
    20 10 20
    Correct answer
  3. C
    20 20 20
  4. D
    10 10 10

Answer

(B) 20 10 20

Approach

The program has a global variable tmp = 20 and a function func() that declares a local static variable also named tmp = 10. C uses static (lexical) scoping, so the tmp referenced in main() is always the global one, while the tmp inside func() is a separate local variable. Trace the three printf calls in execution order.

Reviewed by ISRO IQ Editorial Team.