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?
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.
Post your comment
Please log in or register to post a comment.
Examination Categories
- SSC Exams
- Banking Exams
- Teaching Exams
- Civil Services Exams
- Railways Exams
- Engineering Recruitment Exams
- Defence Exams
- State Government Exams
- Police Exams
- Insurance Exams
- Nursing Exams
- Other Government Exams
- NRA CET
- PG Entrance Exams
- Campus Placement
- MBA Entrance Exams
- CUET and UG Entrance
- Judiciary Exams
- Regulatory Body Exams
- Software Development