ProgrammingHardTCS NQT · Infosys pattern
What is printed?
What is printed?
int x = 1;
void incrementGlobal(void) {
x++;
}
int main(void) {
int x = 10;
incrementGlobal();
printf("%d", x);
}- A. 11
- B. 10
- C. 2
- D. Compile-time error: x is ambiguous
Correct answer: B. 10
incrementGlobal() refers to the file-scope global x (the only x visible from inside that function's own body), incrementing it to 2 — but that is a completely separate variable from main()'s own local x=10, which shadows the global only within main() and is never touched by incrementGlobal(). main() prints its own local x, still 10.
More C Programming questions
- What is printed? c int f(void) { static int x = 10; x += 5; return x; } int main(void) { int a = f(); int b = f(); int c = a + b…
- What happens when these two files are linked together? c / file1.c / extern int shared; void show(void) { printf("%d", shared); } /…
- What is printed? c void f(void) { static int calls = 0; int local = 0; calls++; local++; printf("%d %d ", calls, local); } int main(void)…
- What is printed? c int x = 5; int getX(void) { return x; } int main(void) { int x = 20; printf("%d %d", x, getX()); }
- What is printed? c static int privateCounter = 0; void bump(void) { privateCounter++; } int getCounter(void) { return privateCounter; }…
- What is printed? c int value = 100; void modify(int value) { value = 200; } int main(void) { modify(value); printf("%d", value); }