I was writing a console application that would try to "guess" a number by trial and error, it worked fine and all but it left me wondering about a certain part that I wrote absentmindedly,
The code is:
printf("%d was too big\n",a);
else if (x>a)
printf("%d was too small\n",a);
printf("%d was the right number\n-----------------%d---------------------\n",a,cc);
((100/(i<<=1))? _this space_ :1)
This is a GNU C extension (see ?: wikipedia entry), so for portability you should explicitly state the second operand.
In the 'true' case, it is returning the result of the conditional.
The following statements are almost equivalent:
a = x ?: y; a = x ? x : y;
The only difference is in the first statement,
x is always evaluated once, whereas in the second,
x will be evaluated twice if it is true. So the only difference is when evaluating
x has side effects.
Either way, I'd consider this a subtle use of the syntax... and if you have any empathy for those maintaining your code, you should explicitly state the operand. :)
On the other hand, it's a nice little trick for a common use case.