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?
Answer
(A)
Explanation
Follow the calls in order.
- In
main, the arguments offare evaluated right to left:h(20)runs first and prints 20, theng(10)prints 10. f(10, 20)then runs its body in order:g(x)prints 10 andh(y)prints 20.
Nothing separates the numbers, so the output is 20101020. Left-to-right evaluation would print 10, 20, 10, 20 instead, which is option D. Standard C leaves the order of argument evaluation unspecified, which is why the question fixes it.
Answer: A