| C has many unary operators (operators that take one operand): |
|
But it is almost unique among programming languages in having a ternary one - one that takes three operands.
#define MAX(a,b) (((a)>(b))?(a):(b)) #define MIN(a,b) (((a)<(b))?(a):(b))Remembering that the pre-processor will expand MAX(a,b) as text, it can be seen that this MAX "function" works equally well for int, float and double.
Although it is somewhat cryptic and one could argue that the
? operator merely adds to the difficulty of deciphering
the meaning of the average C program with all its single character
operators and other short-cuts,
the ? operator does allow some compact coding which would be
unnecessarily verbose otherwise.
For example, if you're annoyed by programs that print:
10 cats and 1 dogs
instead of
10 cats and 1 dog
You can fix this by the following artifice:
printf("%d cat%c", n_cats, (n_cats>1)?'s':'\0');
(n_dogs>0)?printf("%d dog%c", n_dogs, (n_dogs>1):'s':'\0'):printf("");
These statements rely on the fact that a null character ('\0')
will be ignored by any output device.
The second statement, while perfectly valid C,
is starting to move into questionable waters.
Many would argue that:
printf("%d cat%c", n_cats, (n_cats>1)?'s':'\0');
if (n_dogs>0) printf("%d dog%c", n_dogs, (n_dogs>1)?'s':'\0');
is better - and I would probably agree with them!
However the extra if .. necessary to avoid an unneeded 's'
in the output definitely adds to the sheer volume of the code
compared to this convenient use of the ? operator.
| Continue on to Functions as Parameters | Back to the Table of Contents |