Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 40

Introduction to C Programming for Embedded Programming

Expressions and Control Flow

C Expressions
Arithmetic operators
* / % + -

What is the result of


10 % 4 10 % 3 / 2

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)
> >= < <= == !=

What is the result of


10 > 5 (unsigned) -1 > 1

Example
if (ch != EOF)
4

C Expressions
Logic operators: Treat non-zero value as TRUE and zero value as FALSE
&& || (AND) (OR)

What is the result of


(10 > 5) && (-1 > -2) (10 > 5) || (-1 > -2) && (2 > 1)

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

any condition is found to be TRUE any condition is found to be FALSE

Why is lazy evaluation important?


Makes code run faster skips unnecessary code Know condition will/will not evaluate, why evaluate other terms Can use lazy evaluation to guard against unwanted conditions Checking for a NULL pointer before using the pointer

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

Byte, Word, Double Word


8 bits, 16 bits, 32 bits
13

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

Converting between forms


Binary to Hexadecimal Easy, every 4 bits is a hexadecimal character 11000100 1100 0100 0x C 4
14

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.

Example: Passed in the value 50 in an 8 bit quantity


Binary = Hex = Decimal =
x = x + 50

00110010 0x32 50

Could be the actual number 50 Could be various bits of information


If bit 6 is set, do something

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)

0x10 ^ 0x10 = 0x00 0x01 ^ 0x10 = 0x11 0xFF ^ 0x00 = 0xFF


To toggle bits: 1. Set the bits you want to toggle to 1 2. Set all other bits to zero (Toggle) (Preserve)
22

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

Conditions and Testing


Comparison Multiple Conditions Tie together using Boolean (logical) operators && AND || OR ! NOT Examples: if ((nVal > 0) && (nArea < 10))

if ((nVal < 3) || (nVal > 50))


if ( ! (nVal <= 10))
25

Conditions and Testing


Test if a single bit is set Clear the other bits and see if the result is zero
Find out if bit 7 of variable nVal is set if (nVal & 0x80) { }
26

Conditions and Testing


Test if any bit is set: Clear the other bits, test if the result is zero See if bit 2 or 3 is set
if (nVal & 0x0C) { }

27

Conditions and Testing


Test if all bits are set:
Clear the other bits, test if the result is the same as the mask

See if bits 2 are 3 are set


if ((nVal & 0x0C) == 0x0C) { }

28

Conditions and Testing


Consider the following definition: short nValue; Write the if statements to test for the following: Bit 13 is true Bit 7, 4, or 0 is true Bits 15 and 0 are true Bits 4 and 2 are false

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

You might also like