- •1 Introduction to C
- •1.1 Some Simple Programs
- •1.2 Names
- •1.3 Types and Type Declarations
- •1.4 Storage Classes, Linkage, and Scope
- •1.5 Character Constants
- •1.6 Arrays
- •1.7 Other types
- •1.8 Operators and Expressions
- •1.9 Increment and Decrement Operators
- •1.10 Precedence and Associativity
- •1.11 Program Flow and Control
- •1.12 Functions
- •1.13 Recursion
- •1.14 Summary
- •2 Advanced C Topics
- •2.1 Pointers
- •2.2 Multidimensional Arrays
- •2.3 Structures
- •2.4 More Structures
- •2.5 Input and Output
- •2.6 Memory Management
- •2.7 Miscellaneous Functions
- •2.8 Summary
- •3 What Are Microcontrollers?
- •3.1 Microcontroller Memory
- •3.3 Programming Microcontrollers
- •3.4 Coding Tips for Microcontrollers
- •4.1 Microcontroller Memory
- •4.2 Timers
- •4.4 Pulse Width Modulator System
- •4.5 Other Program Items
- •4.6 Summary
- •5.1 Header File
- •5.2 Sorting Programs
- •5.3 Data Compression
- •5.4 Timer Operations
- •5.5 Summary
- •6 Large Microcontrollers
- •6.3 A Pulse Width Modulation Program
- •6.4 Cosmic MC68HC16 Compiler
- •6.6 Digital Signal Processor Operations
- •6.7 Other MC68HC16 Considerations
- •7.1 Numeric Encoding
- •7.2 Numeric Decoding
- •7.3 Coding the alpha data
- •7.4 The Monitor Program
- •7.5 The SAVEIT() Routine
- •7.6 The printout() and the printafter() Functions
- •7.7 Reset
- •7.9 Putting It All Together
- •7.10 Summary
- •8 MCORE, a RISC Machine
- •8.1 Delay Routine
- •8.2 Delays Revisited
- •8.4 Handling Interrupts
- •8.5 A Clock Program
- •8.6 Keyboard
- •8.7 Integrating Keyboard and Clock
- •8.8 Adding a Display
- •8.9 Summary
30 Chapter 1 Introduction to C
When this mask is ANDed with r, all of the bits of r, with the exception of the least significant three bits, will be ANDed with a 1, and these bit values will remain unchanged. The least significant three bits will be ANDed with 0 and the result in these three bits will be 0. The bitwise OR will turn bits on. Suppose you wanted to turn bits 2 and 3 of r above on. Here you would use
r = r | 0x0c;
The hexadecimal number 0x0c is a number that has bits 2 and 3 turned on and all other bits turned off. This OR operation will leave bits 2 and 3 on and all other bits will remain unchanged. Suppose that you want to complement a bit in a variable. For example, bit 0 of the memory location PORTA must be toggled each time a certain routine is entered. The expression
PORTA = PORTA ^ 1;
will perform this operation. All of the bits except for bit 1 of PORTA will remain unchanged because the exclusive OR of any bit with a 0 will not change the bit value. However, if bit 1 is 1 in PORTA the exclusive OR will force this bit to 0. If this bit is 0, the exclusive OR will force this bit to a 1. Therefore, the above expression will comple ment bit 0 of PORTA each time it is executed.
The bitwise operators &, |, and ^ are of lower precedence than the equality operators, and higher precedence than the logical AND operator. The bit shift operators are of the same precedence, of lower precedence than the arithmetic operators + and - , and of higher precedence than the relational operators.
Increment and Decrement Operators
When the C language was written, every effort was made to write a language that is concise and yet unambiguous. Several powerful short-hand operators were included in the language that will shorten the program. The increment and decrement operators are examples of such short-hand operators. In the examples earlier there were in stances of expressions such as
i = i + 1;
Increment and Decrement Operators |
31 |
|
|
Here the i value stored in memory is replaced by one more than the value found there at the beginning of execution of the expression. The C expression
++i;
will do exactly the same thing. The increment operator ++ causes 1 to be added to the value in the memory location i. The decrement operator -- causes 1 to be subtracted from the value in the memory location. The increment and decrement operators can be either pre fix or postfix operators. If, like above, the ++ operator precedes the variable, it is called a prefix operator. If the variable is used in an expression, it will be incremented prior to its use. For example, sup pose i = 5. Then the expression
j = 2 * ++i;
will leave a 12 for the value j and 6 for i. On the other hand, if i again is 5, the expression
j = 2 * i--;
will leave a value of 10 for j and 4 for I.
An easy way to see how the preincrement and the postincrement works is as follows: Suppose that you have a pair of statements
j=j+1;
<statement with j>
These statements can be replaced with
<statement with ++j>
The preincrement means that you should replace j with j+1 before you evaluate the expression. Likewise the statements
<statement with j> j=j+1;
can be replaced with
<statement with j++>
with the post increment, you should evaluate the expression and then replace j with j+1.
32 Chapter 1 Introduction to C
Often somebody will wonder what will happen if you have mul tiple increments, either pre or post, of a variable within a single expression. There is an easy answer for that question. Do not do it. The standard provides that between sequence points, an object shall have its value modified at most once and the prior value of the object shall be accessed only to determine its value. Interpretations of the above requirements disallow statements such as
j = j++;
or
a[j] = j++;
or
m = j++ + ++j;
Assignment Operators
Another shorthand that was included in C is called the assign ment operator. When you are programming, you will find that expressions such as
i = i+2;
or
x = x<<1;
are used often. Almost any binary operator can be found on the right side of the expression. A special set of operators was created in C to simplify these expressions. The first expression can be written
i += 2;
and the second
x <<= 1;
These expressions use what is defined as an assignment operator. The operators that can be used in assignment operators are
+ |
>> |
- |
<< |
*&
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.
