5.5Argument evaluation order and return values
The idea
C does not specify the order in which the arguments of a call are evaluated, so prints inside arguments can appear in either order.
Think of it like this: Two cooks work on two sauces for one dish; the standard does not say who starts first.
Why it exists
GATE tells you the assumed order (for example right to left) and expects you to follow it exactly.
How it works
- List the argument expressions.
- Evaluate them in the stated order, performing their side effects (such as printing).
- Only then run the called function body.
- Do not confuse this with the comma operator, which is strictly left to right.
Worked example: f(g(10), h(20)) with right to left
h(20) prints 20, g(10) prints 10, then f runs: g(x) prints 10 and h(y) prints 20. Output 20101020.
Now play with it
- →Open both tabs and compare: right to left gives 20101020, left to right gives 10201020.
The order of evaluating arguments is unspecified in C. GATE 2024 told you to assume right to left: h(20) runs first, then g(10), and only then f.
1#include <stdio.h>2int g(int p) { printf("%d", p); return p; }3int h(int q) { printf("%d", q); return q; }4void f(int x, int y) {5 g(x);6 h(y);7}8int main() {9 f(g(10), h(20));10 printf("\n");11 return 0;12}
The gold line is the statement about to run.
Call stack (top first)
Output
Check yourself
If arguments are evaluated left to right, f(g(1), h(2)) first calls
Exam notes (the dense version)
The order in which the arguments of a call are evaluated is unspecified: f(g(10), h(20)) may call g first or h first. The exam states the assumption (for example "right to left"). The comma between arguments is not the comma operator. Functions return by value; returning the address of a local variable is undefined.
Previous year questions: Functions & Recursion (11)
PYQs are grouped by topic, so this is the full set for Functions & Recursion; try the ones that match this subtopic. Open all 11 on the topic page.