I stumbled into this code for swapping two integers without using a temporary variable or the use of bitwise operators.
int main(){
int a=2,b=3;
printf("a=%d,b=%d",a,b);
a=(a+b)-(b=a);
printf("
a=%d,b=%d",a,b);
return 0;
}
But I think this code has undefined behavior in the swap statement a = (a+b) - (b=a);
as it does not contain any sequence points to determine the order of evaluation.
My question is: Is this an acceptable solution to swap two integers?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…