· The conditional expression operator may be used instead.
· The ternary operator ?: may be used thus:
expr1 ? expr2 : expr3
· Thus, our example may be rewritten:
z = (a > b) ? a : b;
· The right hand side is an expression which has the value of max(a,b). In fact, the ternary expression is often used in macros, e.g.,
· #define MAX(A,B) (((A) > (B)) ? (A) : (B))
· Also, because ?: is an expression, if expr2 or expr3 are of different types, the result is an expression of the highest type. This activity is often called “type promotion.”
· That is, in the code fragment:
Double d = 10.23;
Float f = 3.1416;
int i = 10;
x = (d > 0) ? f : i;
the resulting type of the right-hand side of the assignment to x is float because a mixed expression with float and int becomes float (expr2 is float, expr3 is int).
· The precedence of ?: is just above the assignment operators.
· It associates right to left.
The conditional operator Recall that the conditional operator ?: has the following syntax:
expr1 ? expr2 : expr3
· Also, recall that this operator has the semantics that expr1 is evaluated first.
· Then if expr1 is nonzero (true), expr2 is evaluated and that is the value of the conditional expression as a whole.
· Otherwise, expr1 is zero (false), and expr3 is evaluated making it the value of the conditional expression as a whole.
· Note that this can quickly get out of hand if you’re not careful…
if (y < z)
x = y;
x = z;
x = (y < z)
if (a > 0)
z = 1;
else if (a < 0)
z = -1;
z = 0;
z = (a > 0)
: ((a < 0) ? -1 : 0);
if (a > b)
if (a > c)
z = a;
z = c;
else if (b > c)
z = b;
z = c;
z = (a > b)
? ((a > c) ? a : c)
: ((b > c) ? b : c);
The conditional operator -- example · In the following example, assume that the following declarations are in scope:
char a = 'a'; /* a has an ASCII value of 97 */
char b = 'b'; /* b has an ASCII value of 98 */
int i = 1;
int j = 2;
double x = 7.07;
· Then, the following table illustrates the effect of the conditional operator.
· Exercise: explain why the first expression is an int rather than char
i==j ? a-1 : b+1
(i==j) ? (a-1) : (b+1)
j%3==0 ? i+4 : x
((j%3)==0) ? (i+4) : x
j%3 ? i+4 : x
(j%3) != 0 ? (i+4) : x
Comma operator · When used within an expression, the comma ',' operator, or sequencing operator, is defined thus:
"A pair of expressions separated by a comma is evaluated left to right, and the type and valueof the result are the type and value of the right operand."
· The comma operator has the lowest possible precedence (lower than assignment statements).
· The comma operator associates from left to right.
f (a (), (b (), c ()));
/* call f with two parameters */
· A better way to do this might be the following:
f (a (), c ());
note · The commas that separate function arguments, variables in declarations, etc., are not comma operators, and do not guarantee left to right evaluation. example · The statement
x = (i++, j++, k++);
is exactly equivalent to writing
x = k++;
· Comma operators are commonly used within for loops to initialize more than one looping index, e.g.:
ANSI notes on the comma and other operators in C · The ANSI draft, page 55, states the following about the comma operator:
"The left operand of a comma operator is evaluated as a void expression; there is a sequence point after its evaluation."
· It is also important to note that the comma operator yields an expression and does not yield an lvalue. Thus, the following is incorrect:
(j = 10, k = 20) = 40;
· In other places it states that as && and || guarantee left to right evaluation, there is also a sequence point after evaluating the first operand.
· In general, a semicolon ';' denotes a sequence point, i.e. it is the point at which everything must synch up.
· Up until that point, all subexpressions can be conceptually evaluated in parallel.
Precedence, associativity, and order of evaluation · Precedence, associativity, and order of evaluation are orthogonal concepts, e.g.,