Skip to main content

GATE CSE 2024Programming in C › Functions & Parameter PassingVerified

GATE CSE 2024 PYQ: Consider the following C \text{C} C program. Assume parameters to a function are evaluated from right to…

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

  1. #include <stdio.h>
  2.  
  3. int g(int p) { printf("%d", p); return p; }
  4. int h(int q) { printf("%d", q); return q; }
  5.  
  6. void f(int x, int y) {
  7. g(x);
  8. h(y);
  9. }
  10.  
  11. int main() {
  12. f(g(10), h(20));
  13. }

Which one of the following options is the CORRECT output of the above C\text{C} program?

  1. A
    20101020
    Correct answer
  2. B
    10202010
  3. C
    20102010
  4. D
    10201020

Answer

(A) 20101020 20101020 20101020

Approach

Trace the evaluation order the question prescribes (right-to-left for actual arguments) and then follow the sequence of printf calls executed inside each function body.

With right-to-left argument evaluation the function calls occur in the order h(20), g(10), then the body calls g(10) and h(20), printing 20, 10, 10, 20 with no separators - option A.

Reviewed by GateIQ Editorial Team: IIT & IISc Alumni.