In the following code
int main(){
int a=3;
printf("%d %d %d",++a,a,a++);
return 0;
}
As specified, From C99 appendix C:,
The following are the sequence points described in 5.1.2.3:
The order in which the arguments to a function are evaluated are undefined as specified by the C standard.
However, in the function call for printf, we have arguments that are separated by commas which classify as sequence points. So why does this statement correspond to unspecified behavior?
A comma can only occur between two expressions – commas separate expressions – unlike the semicolon, which occurs at the end of a (non-block) statement – semicolons terminate statements. The comma operator has the lowest precedence of any C operator, and acts as a sequence point.
A sequence point defines any point in a computer program's execution at which it is guaranteed that all side effects of previous evaluations will have been performed, and no side effects from subsequent evaluations have yet been performed.
Pre-C++11 Definitions A sequence point is a point in the execution sequence where all side effects from the previous evaluations in the sequence are complete, and no side effects of the subsequent evaluations started.
Because the comma in the function call is not the comma operator but a separator. So it doesn't introduce any sequence point(s).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With