SMILEY’S WORKSHOP ☺
like you think they should. An
example of the kind of confusion
you can run into is when you use the
‘=’ assignment operator and the ‘==’
‘is equal to’ operator:
You could write this as:
if( temp > 150 )
turnFan(ON);
else
turnFan(OFF);
So the answer is 40, right?
Wrong, according to C, it is – 25.
The compiler does the division and
multiplication first, then the addition
and subtraction:
x = y;
if(x==y) _delay_loop_2(30000);
Or, you could use the C conditional operator ?: as below:
The first statement assigns x the value
of y. The second statement calls the
_delay_loop_2(30000) function if x
‘is equal to’ y. What about:
temp > 150 turnFan(ON) :
turnFan(OFF);
x = 50 + 10 / 2 – 20 4
x = 50 + 10 / 2 – 80
x = 50 + 5 – 80
x = 55 – 80
x = -25
if(x=y) _delay_loop_2(30000); //BAD
STATEMENT
This will set x equal to y and
then call the _delay_loop_2(30000)
function. The ‘if’ is checking to see if
the statement is true, meaning that it
is not equal to 0. In our case, the
delay will always run unless y is 0,
then it will never run. Either way, it
isn’t what you thought you were
testing. The WinAVR compiler will
think something is strange and issue
this warning:
The operation has the form
expresson1 expression2 :
expression3, and follows the rule
that if expression1 is true (non-zero
value), then use expression2,
otherwise use expression3. This
operator seems a little gee-wiz-impress-your-friends and not as clear
as the if-else expression, but you’ll
see this a lot so get used to it.
Precedence and Order of Evaluation
When a statement has a
sequence of operators such as:
x = 50 + 10 / 2 – 20 4;
Warning: suggest parentheses
around assignment used as truth value
which will scroll by so fast you won’t
see it, so you’ll assume the compile
was good. It is a very easy mistake to
make, and you will feel really dumb
after an hour of debugging, looking
for something obscure, only to find a
lousy missing ‘=’ character. I do this
all the time.
the compiler follows an order of calculation based on operator precedence.
But what the compiler does may not
be what you intended. Calculate the
value of x. Did you get 40? If you
performed the calculations sequentially beginning at the left, you get:
Some C gurus will memorize the
precedence and associativity table
and actually write statements like
x = 50 + 10 / 2 – 20 4. Such clever
programmers are dangerous and
should be avoided when possible.
The Germans have a word for clever:
kluge. And in programming, ‘kluge’ is
a well-deserved insult.
Don’t be clever, be clear. Clever
programming is difficult to read and
understand. If the clever programmer
gets run over by a truck (hopefully),
his code will be inherited by some
poor guy who will have to figure
things out. DO NOT memorize the
Table of Operator Precedence and
Associativity in C (which I refuse to
even show). DO use ’(‘ and ‘)’ to
make your program clear!
Which is clearer:
x = 50 + 10 / 2 – 20 4;
or:
x = 50 + 10 / 2 – 20 4
x = 60 / 2 – 20 4
x = 30 – 20 4
x = 10 4
x = 40
x = 50 + (10 / 2) – (20 4);
Assignment Operators
The assignment operators
provide a kind of shorthand technique for arithmetic operations. The
following statements are equivalent:
The second adds nothing for the
compiler, but tells the reader what
Defined
Put the value of y into x
myByte = myByte + yourByte;
Same as:
myByte += yourByte;
Conditional Expressions
You will frequently need to
make decisions based on external
conditions. For example, if the
temperature is above 150° F turn the
fan on, otherwise turn the fan off.
Operator
=
+=
-=
*=
/=
%=
<<=
>>=
&=
^=
|=
Name Example
Assignment x=y
Add x += y
Subtract x -= y
Multiply x *= y
Divide x /= y
Modulo x %= y
Left Shift x <<= y
Right Shift x <<= y
Bitwise AND x &= y
Bitwise XOR x ^= y
Bitwise OR x |= y
TABLE 3. Assignment Operators.
November 2008 63
Compound assignment provides a short
cut way to write an expression, for
example:
x += y; is the same as x = x + y;
x /= y; is the same as x = x/y;