ProgrammingHardHCL · Tech Mahindra pattern
What is printed?
What is printed?
void f(void) {
static int calls = 0;
int local = 0;
calls++;
local++;
printf("%d %d ", calls, local);
}
int main(void) {
f();
f();
f();
}- A. 1 1 1 1 1 1
- B. 1 1 2 1 3 1
- C. 3 3 3 3 3 3
- D. 1 1 1 2 1 3
Correct answer: B. 1 1 2 1 3 1
calls is static and persists, accumulating across calls (1, then 2, then 3). local is an ordinary automatic variable, recreated fresh at 0 every single call, then incremented once to 1 within that call — it never carries over, always printing 1. Output: 1 1 2 1 3 1.
More C Programming questions
- 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); }
- What is printed? c int a = 1; int b = a++ + a++; int c = ++a + ++a; printf("UB UB");
- What is printed? c int a = 5; int b = 10; int result = a > b ? a : b > 0 ? b : 0; printf("%d", result);
- What is printed? c int x = 5; int y = (x = 10) + (x = 20); printf("%d", y);