ProgrammingHardTCS NQT · Infosys · Product Company SDE

What is the output of this C program (assume a typical 64-bit platform: int 32 bits, long and pointers 64 bits)? #include <stdio.h> int f(void) { static int c = 0; c += 2; return c; } int main(void) { int s = 0; for (int i = 0; i < 3; i++) s += f(); printf("%d %d", s, f()); return 0; }

  1. A. 6 2
  2. B. 12 8
  3. C. 18 8
  4. D. 12 2

Correct answer: B. 12 8

The static c persists: successive returns are 2, 4, …, 6; their sum is 12; the next call returns 8.

More C Programming questions

Exams that ask this topic