Ian Zimmerman wrote:
Hello, I hate to make a fool of myself, but:
in C99, is
var = ((exp1) || (exp2));
necessarily equivalent to
int temp = (exp1); var = (temp != 0 ? temp : (exp2));
as I believe it was in pre-99 C?
It was not. || gives you 1 if one of its operands is
not equal to 0.
gcc seems to compile it rather as
var = ((exp1) != 0 || (exp2) != 0)
ie. the final value of var is always 0 or 1.
This is correct behaviour.
The standard did not change in that respect.
If this latter translation is indeed correct, can anyone think of
a way to achieve the old effect without a temporary variable?
This code needs to go into a macro, and exp1 and exp2 are potentially
complex expressions involving function calls and side effects.
Umh, I fear you will need some sort of "var" as above if you
do not want the whole thing too complicated.
I would either go for
(var=(exp1)) || (var=(exp2))
or
(var=(exp1)) ? (var) : (var=(exp2))
and pass var, calling the macro SET_VAR_....(var, ....)
or "return"
(var=(exp1)) ? (var) : (exp2)
<OT>
BTW: Within the last couple of months, there was a discussion
of a GNU extension which would have permitted you solving
your problem by (exp1)?:(exp2). Maybe you can find other
interesting arguments/alternatives there.
http://groups.google.de/groups?selm=....cs.purdue.edu
</OT>
Cheers
Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.