ProgrammingHardIBM · Deloitte pattern

What is printed?

What is printed?
int f(void) {
    static int x = 10;
    x += 5;
    return x;
}
int main(void) {
    int a = f();
    int b = f();
    int c = a + b;
    printf("%d %d %d", a, b, c);
}
  1. A. 15 15 30
  2. B. 15 20 35
  3. C. 10 15 25
  4. D. 20 20 40

Correct answer: B. 15 20 35

static int x = 10; initialises x only ONCE across the whole program; each call to f() adds 5 to the SAME persistent x. First call: x becomes 15, a=15. Second call: x becomes 20, b=20. c = 15+20 = 35.

More C Programming questions

Exams that ask this topic