GATE CS 2024 Set 2 — Question 13

MCQ+1 / -0.33MediumFunctions & RecursionC ProgrammingProgramming & Data Structures

Programming & Data Structures → C Programming → Functions & Recursion

Last updated

Question

Consider the following C program. Assume parameters to a function are evaluated from right to left.
#include 
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?
A.
20101020
B.
10202010
C.
20102010
D.
10201020

Correct answer

(A) 20101020

Solution

The problem specifies that function parameters are evaluated from right to left. In the main function, the call is f(g(10), h(20)).
1.Evaluate arguments of f (Right to Left):
  • First, h(20) is evaluated. It executes printf("%d", 20), printing 20, and returns 20.
  • Next, g(10) is evaluated. It executes printf("%d", 10), printing 10, and returns 10.
  • At this point, the output sequence is 2010.
2. Execute function f(10, 20):
  • Inside f, g(x) is called with x=10. It prints 10.
  • Then, h(y) is called with y=20. It prints 20.
Combining all printed values in order, the final output is 20101020.

More questions on C Programming

Practice GATE CS PYQs with adaptive difficulty

Timed practice, skill tracking, and AI explanations — free to start.

Start practicing free