Page 48 - Programming Microcontrollers in C
P. 48
Increment and Decrement Operators 33
/ ^
% |
If you have two expressions e1 and e2, and let the operand $ repre
sent any binary C operator, then
e1 $= e2;
is equivalent to
e1 = (e1) $ (e2);
The precedence of all of the operator assignments are the same and
less than the precedence of the conditional operator discussed in the
next section. These operators assignments and the = operator are
associated from right to left.
The Conditional Expression
Another code sequence found frequently is
if(exp1)
exp2 ;
else
exp3 ;
The logical expression exp1 is evaluated. If that expression is TRUE,
exp2 is executed. Otherwise, exp3 is executed. In the compact no
tation of C, the above code sequence can be written
exp1 ? exp2 : exp3;
This expression is read if exp1 is TRUE, execute exp2. Otherwise,
execute exp3. Another way of stating this is that if exp1 is TRUE,
the value of the expression is exp2; otherwise the value of the ex
pression is exp3.
The conditional expression is found often in macro definitions,
which we’ll discuss later.
EXERCISES
1. Write a program to determine if a number is even or odd.
2. Write a function that determines the number of bits in an integer
on your machine.