Professional Documents
Culture Documents
Introduction To C Programming For Embedded Programming: Expressions and Control Flow
Introduction To C Programming For Embedded Programming: Expressions and Control Flow
C Expressions
Arithmetic operators
* / % + -
C Expressions
Increment and decrement
++ --
Examples
n++; x = n++; x = ++n; for (i = 0; i < N; i++) for (i = N-1; i > 0; i--)
C Expressions
Relational operators: produce 0 (FALSE) or 1 (TRUE)
> >= < <= == !=
Example
if (ch != EOF)
4
C Expressions
Logic operators: Treat non-zero value as TRUE and zero value as FALSE
&& || (AND) (OR)
Unusual examples
if (10 && 20)
C Expressions
Logic operators are evaluated using lazy evaluation.
Lazy evaluation Once a value completes the condition, stop
OR AND
C Expressions
Statement and expression expression-statement ::= expression ; Are the following valid statements?
10 + 20; average(2, 3); 10; a = b = c = 10; a = 10, b = 20;
C Expressions
Assignment operator: Assignment is a form of an expression Examples
a = (b = (c = 10)) if ((ch = getchar()) == EOF)
C Expressions
L-value: An expression that refers to a region of storage; only l-values can be used at the left side of = operator Are the following valid uses of L-value?
int a; (float) a = 3.14; * ((float*) &a) = 3.14;
C Expressions
Conditional Expressions
expr1 ? expr2 : expr3
Example
x_abs = (x >= 0) ? x : -x;
Is equivalent to
if (x >= 0) x_abs = x; else x_abs = -x;
10
Operator Precedence
~ * + << < == & ^ | && || ! - (unary) / % >> <= > != ++ --
arithmetic
bit shift relational bitwise logical
>=
Boolean
11
Operator Precedence
Bit-wise and X with a MASK, check if the result is zero Is this right?
if (X & MASK == 0)
12
Embedded Programming
Smallest unit of information is a bit
Base 2 notation Two values 1 (TRUE) 0 (FALSE)
Nibble
4 bits 16 possible values Ratio of 1 nibble to 1 hexadecimal character
Embedded Programming
Three most common forms of notation
Decimal (base 10) Hexadecimal (base 16) Binary (base 2) Octal (base 8) 0,1,2,...,9 0,1,2,...,9, A,B,C,D,E,F 0,1 0,1,2,,7
Bitwise Operations
Remember, a notation is just a way of representing a specific quantity. It is up to the programmer to understand/interpret how information is represented and decide how to deal with a variable.
00110010 0x32 50
Could be a combination
If bit 6 is set, x = x + lower nibble of the value
15
Bitwise Operations
Bitwise Operators: Bit manipulation is a key component of embedded programming.
Space Instead of using 8 bits to store one value, now we can use individual bits to store information. Operations are done on a bit by bit basis.
Hence the name bitwise operators/manipulation
Hex notation is the most common form used for bit manipulation.
0xFF is 11111111 in binary 0x10 is 00010000 in binary Recall the binary hex conversion
16
Bitwise Operations
AND Operator & Clear bits and Test bits 0 ANDed with anything will always give a zero 1 ANDed with anything will give the same value
0x10 & 0x10 = 0x10 0x01 & 0x10 = 0x00 0xFF & 0x00 = 0x00 To clear bits: 1. Set the bits you want to clear to zero (Clear) 2. Set all other bits to 1 (Preserve)
17
Bitwise Operations
Example: Clear bits 2, 3 of an 8 bit number Use a mask of 0s at the positions to clear 1111 0011 0x F 3 byVal = byVal & 0xF3;
18
Bitwise Operations
OR Operator | (pipe symbol) Set bits 1 ORed with anything will always give a one 0x10 | 0x10 = 0x10 0x01 | 0x10 = 0x11 0xFF | 0x00 = 0xFF To set bits: 1. Set the bits you want to make a 1 to 1 (Set) 2. Set all other bits to zero (Preserve)
19
Bitwise Operations
Example: Set (to 1) bits 7, 5 of an 8 bit number Use a mask of 1s at the positions to set 1010 0000 0x A 0 byVal = byVal | 0xA0;
20
Bitwise Operations
Suppose we have the following definition: short nVal;
Write the code to do the following: Set bit 13 Clear bit 4 Toggle 15, 14
21
Bitwise Operations
Exclusive OR operator ^ Toggle bits 1 XORed with anything will toggle the bit (not the power sign; to do powers, use the math library)
Bitwise Operations
Inversion/complement operator ~ ~(0x10) =
0xEF;
Shift operators Used to shift a sequence of bits to the left or right >> or << Syntax Variable/Value Operator # of Places nVal = nVal >> 4; /* Shift nVal to the right 4 places */ Why use the shift operator?
Count the number of ones in nVal In a loop, iterate through each bit of nVal
23
Bitwise Operations
Note: The shift operation may be done via an arithmetic shift or by a logical shift Arithmetic MSb stays the same on a right shift Logical Always shift in a zero 0x0F >> 2 0x0F << 2 0x9F >> 1 = = = 0x03; 0x3C;
24
27
28
29
Control Flow
Compound statement
{
declarations; statements; }
Example
if (n > 0) { int count = 0, i; }
30
Control Flow
If-else statement
if (expression) statement1 else statement2
Example
if (n > if (a z = else z = 0) > b) a; b;
Better style
if (n > 0) { if (a > b) z = a; else z = b; }
31
Control Flow
Else-if style
if (expression) statement else if (expression) statement else if (expression) statement else statement
32
Control Flow
Switch statement
switch (expression) { case const-expr: statements case const-expr: statements default: statements }
33
Control Flow
Switch statement example: count space, 0/1, and other characters
switch (ch = getchar()) { case : nspace++; break; case 0: case 1: nbinary++; break; default: nother++; }
34
Control Flow
While loop
while (expression) statement
Example
i = 0, count = 0; while (i < N) { if (X[i++] > 0) count++; }
35
Control Flow
For loop
for (expr1; expr2; expr3) statement
Equivalent
expr1; while (expr2) { statement expr3; }
36
Control Flow
Do-while loop
do
statement while (expression)
Example
i = 0, count = 0; do { if (X[i] > 0) count ++; } while (++i < N);
37
Control Flow
Break: Exit the innermost loop or switch Continue: Start the next iteration immediately
/* skip zeros and stop on any negative */ for (i = 0; i < N; i ++) { if (X[i] == 0) continue; if (X[i] < 0) break; /* process data X[i] */ }
38
Control Flow
Goto and labels They should be only used rarely! state_0: if ((ch = getchar()) == a) goto state_1; else goto state_2; state_1: state_2:
39
Control Flow
Common and tricky bugs: if (x = 0) { } if (x == 0); { }
40