Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C Programming for microcontrollers (Joe Pardue, 2005).pdf
Скачиваний:
260
Добавлен:
12.08.2013
Размер:
4.55 Mб
Скачать

Chapter 4: C Types, Operators, and Expressions

else if( (TCC0RA & WGM01) && (TCC0RA & WGM00) )

{

// do this only if in the Fast PWM mode

}

The (TCC0RA & WGM01) test will be 1, true, only if the WGM01 bit is 1, likewise for the (TCC0RA & WGM00) statement. The !(TCC0RA & WGM01), adding the ‘!’ or NOT to the statement means that it is true only if the innards of the () are false. The ‘if’ statement will only be true if both the first and (logical AND = &&) the second are true. So we’ve used two bitwise ANDs and a logical AND in this statement.

AND I hope it is clear. It isn’t, so get out the pencil and paper computer and work through it till it is. Seriously, when I was editing and reread this section I had a ‘good grief’ moment. But this is critical since we will be doing lots of clearing and setting control register bits. And it is as simple as I can make it, so do carefully walk through the example, pencil and paper in hand and work each example.

Assignment Operators and Expressions

Table 6: Assignment Operators

Operator

Name

Example

Defined

=

Assignment

x=y

Put the value of y into x

+=

Compound

x += y

This provides a short cut way to write and

-=

assignment

 

expression, the example:

*=

 

 

x += y; is the same as

/=

 

 

x = x + y;

%=

 

 

 

<<=

 

 

 

>>=

 

 

 

&=

 

 

 

^=

 

 

 

|=

 

 

 

 

 

 

 

61

Chapter 4: C Types, Operators, and Expressions

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, if it is under 100° F, turn the fan off. You could write this as:

if( temp > 150) Fan(ON);

else

Fan(OFF);

Or you could use the C conditional operator ?: (

Table 3) as below:

temp > 150 ? Fan(ON) : Fan(OFF);

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 expression 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;

The compiler follows an order of calculation based on operator precedence (Table 7). 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 as listed you get:

x = 50 + 10 / 2 – 20 * 4 x = 60 / 2 – 20 * 4

x = 30 – 20 * 4 x = 10 * 4

x = 40

62

Chapter 4: C Types, Operators, and Expressions

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 = 50 + 10 / 2 – 20 * 4 x = 50 + 10 / 2 – 80

x = 50 + 5 – 80 x = 55 – 80

x = -25

Some C gurus will memorize the precedence and associatively 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 Associatively in C. DO use ’(‘ and ‘)’ to make your program clear!

Which is clearer:

x = 50 + 10 / 2 – 20 * 4;

or:

x = 50 + (10 / 2) – (20 * 4);

The second adds nothing for the compiler, but tells the reader what was intended. But what if you really meant to have the operations performed in the order listed? Then you would write:

x = ((((50 + 10) / 2) – 20) * 4);

Which would make x = 40. The parentheses can get mighty confusing, but not nearly as confusing as their absence.

Table 7: Operator Precedence and Associativity in C

Operator Type

Operators

Associativity

Expression

() [] . ->

Left to right

Unary

- + ~ ! * & ++ -- sizeof(type)

Right to left

Multiplicative

* / %

Left to right

Additive

+ -

Left to right

63

Chapter 4: C Types, Operators, and Expressions

Shift

<< >>

Left to right

Relational (inequality)

< <= > >=

Left to right

Relational (equality)

== !=

Left to right

Bitwise AND

&

Left to right

Bitwise XOR

^

Left to right

Bitwise OR

|

Left to right

Logical AND

&&

Left to right

Logical OR

||

Left to right

Conditional

?:

Right to left

Assignment

= *= /= %= += -= <<= >>= &= |=

Right to left

 

^=

 

Sequential evaluation

,

Left to right

64