Consider the following 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 program?
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:
-
Evaluate the second argument
h(20):- The function
h(20)is executed. - It prints
20and returns20. - Current Output:
20
- The function
-
Evaluate the first argument
g(10):- The function
g(10)is executed. - It prints
10and returns10. - Current Output:
2010
- The function
Step 2: Execution of f(10, 20)
Now, the function f(x, y) is called with parameters and :
void f(int x, int y) {
g(x); // g(10)
h(y); // h(20)
}
-
Execute
g(10):- It prints
10. - Current Output:
201010
- It prints
-
Execute
h(20):- It prints
20. - Current Output:
20101020
- It prints
Thus, the final output of the program is 20101020, which corresponds to option A.
Free practice · Your answers on this page are not saved.
