I happened to meet this code:
emmmm....Code:#include <stdio.h> int main(){ int count = 0;; printf("%d%d%d%d",++count ,++count ,++count ,++count ) ; } I think the result is 1234. But the result is
I happened to meet this code:
emmmm....Code:#include <stdio.h> int main(){ int count = 0;; printf("%d%d%d%d",++count ,++count ,++count ,++count ) ; } I think the result is 1234. But the result is
It's undefined behaviour. You're not allowed to modify an object more than once between sequence points.
Even if it was defined, the order in which the parameters are evaluated is not defined so it could just as well be 4321.
4444 is an interesting result and shows that the implementation is definitely relying on you only modifying an object once within the parameters since it performs all the increments before pushing the values for the call.
BTW, your compiler should give you a warning about that line. With gcc use the -Wall switch for more warnings.
emmmm......That is to say if I use ++ operator,I cannot predict the value of the parameters?
> That is to say if I use ++ operator,I cannot predict the value of the parameters?
No, if you use the ++ or -- on the same variable more than once (between sequence points), you're on your own.
printf("%d %d\n", ++x, y++);
is perfectly fine, because each variable has only one side effect between sequence points.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.