conditional operator :? with function calls
consider the statement :
Does :? need to call and evaluate both functions to execute, or does it just
i >= 0 ? positive_case() : negative_case();
call and evaluate one or the other function according to the conditional expression?
It's confusing me because I don't know how function calls are placed in the
hierarchy of order of operations. If you consider the absolute value of integer i in this statement :
It can't be written like this :
abs_i = i >= 0 ? i : -i;
because the assignment operator is of lower precedence than :?. You need
i >= 0 ? abs_i = i : abs_i = -i; /* doesn't work */
which implies that :? must first evaluate all its operands. So, I guess what I'm asking is, is it bad programming practice to use function calls in such an implementation? I know how it behaves on my compiler, but I don't know what standard C guarantees of this.
i >= 0 ? (abs_i = i) : (abs_i = -i); /* works */