The GATE Grind

GATE 2024 CS (CS2) – Question 13

Programming and Data Structures · Programming in C · 1 mark · Multiple choice

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. 20101020
  2. 10202010
  3. 20102010
  4. 10201020

Practise this question in The GATE Grind →

Show answer and explanation

Correct answer: (A) 20101020

Explanation

Right-to-left evaluation calls h(20) first (prints 20), then g(10) (prints 10). Then f runs g(10) then h(20): prints 10 20. Output: 20101020.