ProgrammingHardIBM · Deloitte pattern
What is printed?
What is printed?
void f(void) {
static int x = 0;
x += 10;
printf("%d ", x);
}
int main(void) {
for (int i = 0; i < 3; i++) f();
}- A. 10 10 10
- B. 0 10 20
- C. 30 30 30
- D. 10 20 30
Correct answer: D. 10 20 30
static x is initialized to 0 only once; each call adds 10 and the value persists between calls, producing 10, then 20, then 30 across the three loop iterations.
More C Programming questions
- What is printed? c int global = 1; void modify(void) { int global = 2; global++; } int main(void) { modify(); printf("%d", global); }
- What is printed? c #include <stdio.h> int x; // tentative definition, file scope int x; // another tentative definition, same file int…
- What is printed? c static int x = 5; void f(void); void f(void) { printf("%d", x); } int main(void) { f(); }
- What is printed? c int a = 2, b = 3, c = 4; int r = a + b c - a / b; printf("%d", r);
- What is printed? c int a = 5; int r = a > 3 ? 10 : 20 + 5; printf("%d", r);
- What is printed? c int a = 4; int r = ~a + 1 == -a; printf("%d", r);