View
17
Download
1
Category
Preview:
Citation preview
2 © tjEE 1910
Expressions and Precedence
• Expression: Sequence of Operators and Operands that reduce to a single value
• Simple and Complex Expressions
• Subject to Precedence and Associativity
• Six categories• Primary• Postfix• Prefix• Unary• Binary• Ternary
3 © tjEE 1910
Expressions and Precedence
• Simple Expressions
• Only 1 operator
• a + b
• Complex Expressions
• Multiple operators
• 2*3/6
4 © tjEE 1910
Expressions and Precedence
• Primary Expressions
• One operand and no operators
• Namea interest_rate RATE initial1
• Literal2 123.456 ‘a’ “hello ee1910”
• ParentheticalAnything in parentheses reduces to a single value
(2 + 3 * 4) (a = b + c)
5 © tjEE 1910
Expressions and Precedence
• Postfix Expressions
• One operand followed by one operator• Operand must be a variable
• Function CallFunction name is an operand (named entity)
Parenthesis are the operator
printf(…)
• Postfix increment/decrementi++ → i = i + 1
j-- → j = j - 1
6 © tjEE 1910
Expressions and Precedence
• Postfix Expressions
• Some expressions have a Value and a Side Effect
int j;
int x;
j = 5;
x = j++;
Value: x = 5
Side Effect: j = 6
• Postfix indicates to operate after the evaluation
Consider printf(“%d”, j++);
5 or 6 ?
7 © tjEE 1910
Expressions and Precedence
• Prefix Expressions
• One operator followed by one operand• Operand must be a variable
• Only 2 examples
• Prefix increment/decrement++j → j = j + 1
--k → k= k - 1
8 © tjEE 1910
Expressions and Precedence
• Prefix Expressions
• Some expressions have a Value and a Side Effect
int j;
int x;
j = 5;
x = ++j;
Value: x = 6
Side Effect: j = 6
• Prefix indicates to operate before the evaluation
Consider printf(“%d”, ++j);
5 or 6 ?
9 © tjEE 1910
Expressions and Precedence
• Unary Expressions
• One operator followed by one operand• Operand can be any expression
+, -
cast
sizeof(int). …
a = 5
+a -> +5 Note: the expression is modified
-a -> -5 not the variable, a = 5 in both cases
10 © tjEE 1910
Expressions and Precedence
• Binary Expressions
• Operand operator operand• Familiar to us: +, -, *, /
• New: % - modulus (remainder of a division)
• Subject to type limitations
10 * 2 → 20 10 / 2 → 5
true * 2 → 2 true / 2 → 0 ???
‘A’ * 2 → 130 ‘A’ / 2 → 32 ???
15.6 * 2 → 31.2 15.6 / 2 → 7.8
3 / 5 → 0 12 / 5 → 2
3 % 5 → 3 12 % 5 → 2
Modulo only operates on integers
11 © tjEE 1910
Expressions and Precedence
• Binary Expressions
• Special binary expression - assignment• variable = expression• Has both a value - result of right side• And a side effect – places value into the variable on the left
side
• Simplea = b + c j = j * 2
• Compound*=, /=, +=, -=, %=a *= b → a = a * ba += 10 → a = a + 10a -= b + c → a = a – (b + c) expression is evaluated first
13 © tjEE 1910
Expressions and Precedence
• Precedence
• Order in which operators are evaluated• In math: * and / before + and –
• 2/3+3*4 → ((2/3) + (3*4))
• Associativity
• Order in which operators with the same precedence are evaluated• In math: left to right
• 2 + 3 – 4 + 5 → (((2 + 3) – 4) + 5)
14 © tjEE 1910
Expressions and Precedence
Precedence Operator Description Associativity
1
++ -- Suffix/postfix increment and decrement Left-to-right
() Function call
[] Array subscripting
. Structure and union member access
-> Structure and union member access through pointer
(type){list} Compound literal(C99)
2
++ -- Prefix increment and decrement Right-to-left
+ - Unary plus and minus
! ~ Logical NOT and bitwise NOT
(type) Type cast
* Indirection (dereference)
& Address-of
sizeof Size-of
_Alignof Alignment requirement(C11)
Precedence Operator Description Associativity
1
++ -- Suffix/postfix increment and decrement Left-to-right
() Function call
[] Array subscripting
. Structure and union member access
-> Structure and union member access through pointer
(type){list} Compound literal(C99)
2
++ -- Prefix increment and decrement Right-to-left
+ - Unary plus and minus
! ~ Logical NOT and bitwise NOT
(type) Type cast
* Indirection (dereference)
& Address-of
sizeof Size-of
_Alignof Alignment requirement(C11)
3 * / % Multiplication, division, and remainder Left-to-right
4 + - Addition and subtraction
5 << >> Bitwise left shift and right shift
6< <= For relational operators < and ≤ respectively
> >= For relational operators > and ≥ respectively
7 == != For relational = and ≠ respectively
8 & Bitwise AND
9 ^ Bitwise XOR (exclusive or)
10 | Bitwise OR (inclusive or)
11 && Logical AND
12 || Logical OR
13 ?: Ternary conditional Right-to-Left
14
= Simple assignment
+= -= Assignment by sum and difference
*= /= %= Assignment by product, quotient, and remainder
<<= >>= Assignment by bitwise left shift and right shift
&= ^= |= Assignment by bitwise AND, XOR, and OR
15 , Comma Left-to-right
15 © tjEE 1910
Expressions and Precedence
Precedence Operator Description Associativity
1
++ -- Suffix/postfix increment and decrement Left-to-right
() Function call
[] Array subscripting
. Structure and union member access
-> Structure and union member access through pointer
(type){list} Compound literal(C99)
2
++ -- Prefix increment and decrement Right-to-left
+ - Unary plus and minus
! ~ Logical NOT and bitwise NOT
(type) Type cast
* Indirection (dereference)
& Address-of
sizeof Size-of
_Alignof Alignment requirement(C11)
Precedence Operator Description Associativity
1
++ -- Suffix/postfix increment and decrement Left-to-right
() Function call
[] Array subscripting
. Structure and union member access
-> Structure and union member access through pointer
(type){list} Compound literal(C99)
2
++ -- Prefix increment and decrement Right-to-left
+ - Unary plus and minus
! ~ Logical NOT and bitwise NOT
(type) Type cast
* Indirection (dereference)
& Address-of
sizeof Size-of
_Alignof Alignment requirement(C11)
3 * / % Multiplication, division, and remainder Left-to-right
4 + - Addition and subtraction
5 << >> Bitwise left shift and right shift
6< <= For relational operators < and ≤ respectively
> >= For relational operators > and ≥ respectively
7 == != For relational = and ≠ respectively
8 & Bitwise AND
9 ^ Bitwise XOR (exclusive or)
10 | Bitwise OR (inclusive or)
11 && Logical AND
12 || Logical OR
13 ?: Ternary conditional Right-to-Left
14
= Simple assignment
+= -= Assignment by sum and difference
*= /= %= Assignment by product, quotient, and remainder
<<= >>= Assignment by bitwise left shift and right shift
&= ^= |= Assignment by bitwise AND, XOR, and OR
15 , Comma Left-to-right
* & and () have multiple definitionsUsage is context dependent
16 © tjEE 1910
Expressions and Precedence
• Examples (ints)
a = 2, b=3, c=4
1 + 2 * 3 →
1 + 2 * 3 / 2 →
-b++ →
a += b *= c -= 3 →
--a * (1 + b) / 3 – c++ * b →
17 © tjEE 1910
Expressions and Precedence
• Examples
a = 2, b=3, c=41 + 2 * 3 → 1 + (2 * 3) = 71 + 2 * 3 / 2→ 1 + ((2 *3) /2) = 1 + (6/2) = 4
same precedence (L-R)-b++ → -(b++) = -3 evaluates first (b is now 4)a += b *= c -= 3 → c= 1, b=3, a=5 same precedence (R-L)
--a * (1 + b) / 3 – c++ * b →
--a * 4 / 3 – c++ * b --a * 4 / 3 – 4 * b1 * 4 / 3 – 4 * b4 / 3 – 4 *31 - 12-11 Answers
Answers
18 © tjEE 1910
Expressions and Precedence
• Precedence and Associativity
For clarity and precision
Use Parenthesis freely
(((--a) * (1 + b) )/ 3) – ((c++) * b) →
((( 1 ) * ( 4 )) / 3 ) – (( 4 ) * 3 )(( 4 / 3 ) – ( 12 ))( 1 – 12 )-11
a = 2, b=3, c=4
19 © tjEE 1910
Number Systems
• Special note on binary numbers in C programming
• Some but not all compilers allow binary numbers to be represented in C code directly
95 → 0b01011101
• To be safe and ensure our code is portables we will NOTuse this notation.
• Binary numbers can be represented with:• Their decimal equivalents 95
• Their hexadecimal equivalents 0x5D
20 © tjEE 1910
Expressions and Precedence
• Bitwise Operatorsa = 0x86, b = 0xA5, c = -35 (8 bit values)
• ~ bitwise not• inverts the individual bits in a number• ~a→ ~(1000 0110) → 0111 1001 → 0x79• ~c→ ~(1101 1101) → 0010 0010 → 34
• | bitwise or• ORs the individual bits• a | b → (1000 0110) | (1010 0101) → 1010 0111 → 0xA7
• & bitwise and• ANDs the individual bits• a & b → (1000 0110) & (1010 0101) → 1000 0100 → 0x84
• ^ bitwise xor• XORs the individual bits• a ^ b → (1000 0110) ^ (1010 0101) → 0010 0011 → 0x23
21 © tjEE 1910
Expressions and Precedence
• Bitwise Operatorsuint8_t a;
int8_t b;
a = 0xA6;
b = 0xA6;
• >> bitwise shift right• shifts the individual bits in a number to the right
• a >> 2 → (1010 0110) >> 2 → 0010 1001 - unsigned
OR
• b >> 2 → (1010 0110) >> 2 → 1110 1001 - signed
• << bitwise shift left• shifts the individual bits in a number to the left
• a << 3 → (1010 0110) << 3 → 0011 0000 - unsigned or signed
22 © tjEE 1910
Expressions and Precedence
• Logical Operators
• ! logical not• inverts the logical value• !true → false• !34 → false
• || logical OR• evaluates both sides logically then does an OR• true || false → true• 0 || 0 → false• 34 || -32 → true
• && logical AND• evaluates both sides logically then does an AND• true && true → true• 1 && 0 → false• 34 && -32 → true
Reminder: The ONLY integer valuethat is false is 0
23 © tjEE 1910
Expressions and Precedence
• Relational Operators
• ==, <, >, <=, >=, !=• equals, LT, GT, LE, GE, not equal
• evaluates by type
• evaluates to Boolean T or F
• true == true → true
• 34 == 32 → false
• 34 != 32 → true
• 5 >= 5 → true
• 3.5 <= 12 → true
• true < 5 → true
• true == -15 → true
Recommended