# Thread: conditional operator :? with function calls

1. ## conditional operator :? with function calls

consider the statement :

Code:
`i >= 0 ? positive_case() : negative_case();`
Does :? need to call and evaluate both functions to execute, or does it just
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 :

Code:
`abs_i = i >= 0 ? i : -i;`
It can't be written like this :

Code:
`i >= 0 ? abs_i = i : abs_i = -i;     /* doesn't work */`
because the assignment operator is of lower precedence than :?. You need
parenthesis :

Code:
`i >= 0 ? (abs_i = i) : (abs_i = -i);     /* works */`
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.

2. Does :? need to call and evaluate both functions to execute, or does it just
call and evaluate one or the other function according to the conditional expression?
Check it out for yourself:
Code:
`i >= 0 ? positive_case() : negative_case();`
Code:
```
int positive_case() { printf("positive case\n") ; return 1 ; }
int negative_case() { printf("negative case\n") ; return 0 ; }```

3. A bit slow...
Code:
```int positive_case(void)
{
printf("Positive case\n");
return 1;
}
int negative_case(void)
{
printf("negative case\n");
return -1;
}

i >= 0 ? positive_case() : negative_case();
// equavilent to
if( i >= 0) {
positive_case();
} else {
negative_case();
}
// also equavilent to .
(i >= 0 ? positive_case : negative_case) ();      // don't try it @ home :D```
Yes it does *NOT* evaluate both operands.

4. which implies that :? must first evaluate all its operands
You're treating C as though it was a dynamic scripting language or a dialect of lisp. The compiler is trying to turn complex C expressions into very 'atomic' machine instructions. The compiler has to process the entire expression and evaluate the precedence of operators, in order to turn your long expression into a sequence of very simple operations. So your compiler is, in a sense, evaluating both expressions, but that doesn't mean that the machine code from both expressions will get run.

5. @sean, exactly

6. I get it now. Thanks guys.

7. Originally Posted by Dino
Check it out for yourself:
Code:
`i >= 0 ? positive_case() : negative_case();`
Code:
```
int positive_case() { printf("positive case\n") ; return 1 ; }
int negative_case() { printf("negative case\n") ; return 0 ; }```
One can of course do that, but it doesn't guarantee that the outcome is defined by the standard, which could mean that what it does with that code on one compiler it doesn't do on another.

Fortunately, I myself have asked this very question before on another forum and can tell you that it is guaranteed by the standard that only one of the last two arguments is evaluated.

8. ? : is just like any other if-else statement combination. If the first one is true, the second one isn't evaluated.

Quzah.