I am not really that advanced in C but I found this in a piece of code
ssize_t ret = 0;
ret += sprintf(buf + --ret, "\n");
What does this mean?
And can we really do --ret
in this case?
ret += sprintf(buf + --ret, "\n");
The above line does not mean anything, because it is Undefined Behavior (UB).
Thus, the answer is an emphatic No!.
Why?
Because ret
is written twice, and there is no intervening sequence-point.
(The actual increment of ret
can happen at any time after reading it to determine the value of --ret
, and before the closing semicolon.
Thus, it conflicts with the change by the assignment ret += ...
.
Also, the read of ret
in ret += ...
conflicts with the update of ret
in --ret
.)
As an aside, in C++ since C++11 (sequencing instead of sequence-points) it would be well-defined.