I don't understand what is passed to the function f() when I call it like this.
main()
{
void f(int,int);
int i=10;
f(i,i++);
}
void f(int i,int j)
{ printf("%d %d",i,j); }
gives me 11 10 .Can somebody explain why its 11?
Software Engineering Stack Exchange is a question and answer site for professionals, academics, and students working within the systems development life cycle. It only takes a minute to sign up.
Sign up to join this communityThe behavior is undefined, as follows:
From the C99 standard:
6.5 Expressions
...
2 Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression.72) Furthermore, the prior value shall be read only to determine the value to be stored.73)
72) A floating-point status flag is not an object and can be set more than once within an expression.
73) This paragraph renders undefined statement expressions such aswhile allowingi = ++i + 1; a[i++] = i;
i = i + 1; a[i] = i;
We're violating the second sentence of that paragraph; we're not just reading the prior value to determine the new value to be stored.
Note that undefined doesn't necessarily mean illegal. Undefined simply means that the compiler is free to handle the situation any way it sees fit; any result is considered "correct". You could very well wind up with the result you expected. Or not. The program could crash. Or not. Or anything else could happen.
In this case, you will get different results based on platform, compiler, optimization settings, surrounding code, etc. The reason for that is as follows:
6.5.2.2 Function calls
...
10 The order of evaluation of the function designator, the actual arguments, and subexpressions within the actual arguments is unspecified, but there is a sequence point before the actual call.
It is unspecified whether i
or i++
is evaluated first; furthermore, it is unspecified whether the side effect of ++
is applied immediately after i++
is evaluated, as follows:
6.5.2.4 Postfix increment and decrement operators
...
2 The result of the postfix++
operator is the value of the operand. After the result is obtained, the value of the operand is incremented. (That is, the value 1 of the appropriate type is added to it.) See the discussions of additive operators and compound assignment for information on constraints, types, and conversions and the effects of operations on pointers. The side effect of updating the stored value of the operand shall occur between the previous and the next sequence point.
Emphasis mine.
Edit
Changed the wording a bit to make it clear that the behavior is undefined.
f(*a, (*b)++);
where both a
and b
are set to point to i
? In a different translation unit?
May 29, 2012 at 11:06
Because undefined behaviour, that's why. You read the value twice and modify it once with no intervening sequence point, which is a giant pile of illegal. Even if it were not illegal, the compiler has no obligation to evaluate your function arguments in any particular order, or indeed, to even evaluate one fully before evaluating another.
i
either before or after i++
.
May 28, 2012 at 20:25
foo(bar(), baz());
is perfectly well defined, but may call either bar
or baz
first.
The order of evaluation of function arguments is unspecified and so 11 10
is a correct possible output.
f(i,++i);
and compared it tof(i,i++);
?