Expressions and Precedence Last updated 12/10/18
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 2 tj
Simple Expressions Only 1 operator a + b Complex Expressions Multiple operators 2*3/6 3 tj
Primary Expressions One operand and no operators Name a interest_rate RATE initial1 Literal 2 123.456 a hello ee1910 Parenthetical Anything in parentheses reduces to a single value (2 + 3 * 4) (a = b + c) 4 tj
Postfix Expressions One operand followed by one operator Operand must be a variable Function Call Function name is an operand (named entity) Parenthesis are the operator printf( ) Postfix increment/decrement i++ i = i + 1 j-- j = j - 1 5 tj
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 Consider printf( %d, j++); 5 or 6? Postfix indicates to operate after the evaluation 6 tj
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 7 tj
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 Consider printf( %d, ++j); 5 or 6? Prefix indicates to operate before the evaluation 8 tj
Unary Expressions One operator followed by one operand Operand can be any expression sizeof(int) sizeof(x) a = 5 +a -> +5 Note: the expression is modified -a -> -5 not the variable, a = 5 in both cases char x; (int) x the value is cast as an int x is not cast as an int 9 tj
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 in integers 10 tj
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 Simple a = b + c j = j * 2 Compound *=, /=, +=, -=, %= a *= b a = a * b a += 10 a = a + 10 a -= b + c a = a (b + c) expression is evaluated first 11 tj
Ternary Expressions 12 tj
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) 13 tj
Precedence Operator Description Associativity ++ -- Suffix/postfix increment and decrement Left-to-right () Function call [] Array subscripting 1. Structure and union member access -> Structure and union member access through pointer Precedence (type){list} Operator Compound literal(c99) Description Associativity ++ -- Prefix increment and decrement ++ -- Suffix/postfix increment and decrement Left-to-rightRight-to-left + - Unary plus and minus! ~ () Logical NOT and bitwise Function NOT call (type) [] Type cast 2 Array subscripting * Indirection (dereference) 1 &. Address-of Structure and union member access sizeof Size-of _Alignof -> Alignment requirement(c11) Structure and union member access through pointer 3 * / % Multiplication, division, and remainder Left-to-right (type){list} Compound literal(c99) 4 + - Addition and subtraction 5 << >> ++ Bitwise -- left shift Prefix and right increment shift and decrement Right-to-left < <= + For - relational operators Unary plus < and and respectively 6 minus > >= For relational operators > and respectively! ~ Logical NOT and bitwise NOT 7 ==!= For relational = and respectively 8 & (type) Type cast 2 Bitwise AND 9 ^ * Bitwise XOR (exclusive Indirection or) (dereference) 10 & Bitwise OR (inclusive Address-of or) 11 && Logical AND sizeof Size-of 12 Logical OR 13?: _Alignof Ternary conditional Alignment requirement(c11) Right-to-Left = Simple assignment += -= Assignment by sum and difference 14 *= /= %= 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 14 tj
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 15 tj
Examples a = 2, b=3, c=4 1 + 2 * 3 1 + (2 * 3) = 7 1 + 2 * 3 / 2 1 + ((2 *3) /2) = 1 + (6/2) = 4 same precedence Answers (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 * b 1 * 4 / 3 4 * b Answers 4 / 3 4 *3 1-12 -11 16 tj
Precedence and Associativity For clarity and precision Use Parenthesis freely (((--a) * (1 + b) )/ 3) ((c++) * b) ((( 1 ) * ( 4 )) / 3 ) (( 4 ) * 3 ) (( 4 / 3 ) ( 12 )) Answers ( 1 12 ) -11 17 tj
Bitwise Operators ~ bitwise not inverts the individual bits in a number ~(1001 0110) 0110 1001 bitwise or ORs the individual bits (1001 1001) (1010 1100) 1011 1101 & bitwise and ANDs the individual bits (1001 1001) & (1010 1100) 1000 1000 ^ bitwise xor XORs the individual bits (1001 1001) ^ (1010 1100) 0011 0101 18 tj
Bitwise Operators >> bitwise shift right shifts the individual bits in a number to the right (1001 1001) >> 2 0010 0110 - unsigned OR 1110 0110 - signed << bitwise shift left shifts the individual bits in a number to the left (1001 1001) << 2 0110 0100 - unsigned or signed 19 tj
Logical Operators! logical not inverts the logical value!true false!34 false logical OR evaluates both sides logically then does an OR true true 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 20 tj
Relational Operators ==, <, >, <=, >=,!= equals, LT, GT, LE, GE, not equal evaluates the parameters by type true == true true 34 == 32 false 34!= 32 true 5 >= 5 true 21 tj