Machine-translated by Google. Questions are authored in English.
Top

Discussion

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

#include \(\lt\)stdio.h\(\gt\)

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

Answer: A

The output is 20101020.

In f(g(10),h(20)) the arguments are evaluated before f runs, and the question fixes their order as right to left. So h(20) runs first and prints 20, then g(10) prints 10; they return 20 and 10, which become y and x.

Inside f, g(x) prints 10 and then h(y) prints 20. No separators are printed, so the whole output reads 20, 10, 10, 20 run together: 20101020.

10202010 evaluates the arguments left to right and then swaps the body. 20102010 gets the arguments right but reverses the calls inside f, and 10201020 is the answer for left-to-right evaluation.

No comment is present. Be the first to comment.
Loading…

Post your comment

Please log in or register to post a comment.