Are are any significant difference between the following two satements ?
1) if ( 0 == strcmp(str1, str)) ..
2) if ( strcmp(str1, str) == 0)) ...
Are are any significant difference between the following two satements ?
1) if ( 0 == strcmp(str1, str)) ..
2) if ( strcmp(str1, str) == 0)) ...
Only that the first is a lot more unreadable than the second.
The supposed advantage is that it won't compile if you do if ( 0 = var )
Question 17.4
But since strcmp(x,y) = 0 would also not compile, it is a moot point in this case.
Where people got all out of shape over it was with if ( var = 0 ) becoming if ( 0 = var ), which would indeed be a good thing.
So they apply the same rule to lots of other cases, like swapping if ( i < 10 ) into if ( 10 > i ), which makes almost no sense at all when read out loud.
But when you have if ( var1 = var2 ), then the safety net has gone, and no amount of rearranging will save you from not knowing about ==
It was a semi-popular idea in the 1980's (like dumb xor-swaps were). But nowawdays, any half-decent compiler will warn you if you do if ( var = something ), even for the troublesome case of if ( var1 = var2 )
For me, the first order for a program is that it should be readable. That is, consistently indented and using familar idioms.
Syntactic tricks to solve a problem which doesn't exist waste the time of EVERY person who has to read the code. Code gets read a lot more times than it is written.
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.