GATE CS 2024 Set 2 | Question 13

MCQ · 1 mark · −0.33

Consider the following C program. Assume parameters to a function are evaluated from right to left.

#include <stdio.h>

int g(int p) { printf("%d", p); return p; }
int h(int q) { printf("%d", q); return q; }

void f(int x, int y) {
  g(x);
  h(y);
}

int main() {
  f(g(10),h(20));
}

Which one of the following options is the CORRECT output of the above C program?

  1. Option A: 20101020 (correct)
  2. Option B: 10202010
  3. Option C: 20102010
  4. Option D: 10201020

Answer

(A)

Explanation

Follow the calls in order.

  • In main, the arguments of f are evaluated right to left: h(20) runs first and prints 20, then g(10) prints 10.
  • f(10, 20) then runs its body in order: g(x) prints 10 and h(y) prints 20.

Nothing separates the numbers, so the output is 20101020. Left-to-right evaluation would print 10, 20, 10, 20 instead, which is option D. Standard C leaves the order of argument evaluation unspecified, which is why the question fixes it.

Answer: A