ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
int globalVar = 100;
void modify(void) {
    int globalVar = 200;
    globalVar += 5;
    printf("local:%d ", globalVar);
}
int main(void) {
    modify();
    printf("global:%d", globalVar);
}
  1. A. local:105 global:105
  2. B. local:205 global:100
  3. C. local:205 global:205
  4. D. local:105 global:100

Correct answer: B. local:205 global:100

modify() declares its own local globalVar=200, shadowing the global entirely within its scope; `globalVar += 5` only affects this local copy (205), leaving the true file-scope globalVar completely untouched at 100.

More C Programming questions

Exams that ask this topic