ProgrammingHardTCS NQT · Product company OA

What is printed?

What is printed?
#define CUBE(x) ((x) * (x) * (x))
int n = 0;
int f(void) { n++; return 2; }
int main(void) {
    int r = CUBE(f());
    printf("%d %d", r, n);
}
  1. A. 27 3
  2. B. 6 3
  3. C. 8 1
  4. D. 8 3

Correct answer: D. 8 3

Full parentheses protect precedence but not side effects: the argument text f() is pasted three times, so f runs three times. Each call returns 2, so r = 8 and n = 3. A function cube(f()) would call f once.

More C Programming questions

Exams that ask this topic