Skip to content
Official PYQ · 2024

GATE CS 2024 Set 2 Q3 — Programming in C

Explain with AI

Consider the following C\text{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\text{C} program?

Choose one option
View worked solution

Answer: A

To determine the output of the given C program, we trace the execution step-by-step, keeping in mind that function arguments are evaluated from right to left.

Step 1: Evaluation of arguments in main()

The main() function calls f(g(10), h(20)). Since arguments are evaluated from right to left:

  1. Evaluate the second argument h(20):
    • The function h(20) is executed.
    • It prints 20 and returns 20.
    • Current Output: 20
  2. Evaluate the first argument g(10):
    • The function g(10) is executed.
    • It prints 10 and returns 10.
    • Current Output: 2010

Step 2: Execution of f(10, 20)

Now, the function f(x, y) is called with parameters x=10x = 10 and y=20y = 20:

void f(int x, int y) {
    g(x); // g(10)
    h(y); // h(20)
}
  1. Execute g(10):
    • It prints 10.
    • Current Output: 201010
  2. Execute h(20):
    • It prints 20.
    • Current Output: 20101020

Thus, the final output of the program is 20101020, which corresponds to option A.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Programming in C questions