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);
}- A. local:105 global:105
- B. local:205 global:100
- C. local:205 global:205
- 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
- What is printed? c int a = 3; int result = a++ 2 + --a; printf("%d %d", a, result);
- What is printed? c // header.h (included by both files) extern int sharedValue; c // a.c #include "header.h" int sharedValue = 42; c //…
- What is printed? c int a = 5, b = 2, c; c = a > b ? a++ : --b; printf("%d %d %d", a, b, c);
- What is printed? c int a = 2; int b = (a++, a + 3, a 2); printf("%d %d", a, b);
- What is printed? c int a = 10; int b = a++ + a++; printf("%d", a);
- What is printed? c int x = 5; if (x = 10) printf("true %d", x); else printf("false %d", x);