ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU CS Status 6/19/2015 Initial content copied verbatim from ECE 103 material developed by Professor Phillip PSU ECE
Syllabus What’s This Blue Code? Operator Precedence and Associativity Arithmetic Operators Relational and Logical Operators Pre- Post-Increment, Decrement Operators Assignment Operators Bitwise Operators Type Conversion (Casting) Memory Access Operators
2 What’s This Blue Code? #include // now your function uses C library function void foo( void ) { // foo printf( ”Hello world!\n” ); } //end foo int main( /* no params */ ) { // main foo();// clumsy to call function, but OK return 0;// no error } //end main
3 Operator Precedence and Associativity Operators perform basic arithmetic, comparison, and logic operations When operators are combined, rules of precedence and associativity determine the evaluation order Precedence Rules → specifies which operator is evaluated first when operators of different precedence are adjacent; e.g. multiply * before addition + Associativity Rules → specifies which operator is evaluated first when operators of the same precedence are adjacent; e.g. left-to-right, or right-to-left, or none whatsoever (e.g. IBM’s APL)
4 Table 1: C Operator Precedence Table LDescriptionOperatorAssociativityLDescriptionOperatorAssociativity 1 Function call ( ) left-to-right 5 Bitwise left shift << left-to-right Array subscript [ ] Bitwise right shift >> Struct member. 6 Less than < left-to-right Struct dereference -> Greater than > Increment (post) expr++ LT or equal to <= Decrement (post) expr-- GT or equal to >= 2 Indirection * right-to-left 7 Equal to == left-to-right Reference (addr) & Not equal to != Unary plus + 8Bitwise AND & left-to-right Unary minus - 9Bitwise XOR ^ left-to-right Logical negation ! 10Bitwise OR | left-to-right Bitwise NOT ~ 11Logical AND && left-to-right Increment (pre) ++expr 12Logical OR || left-to-right Decrement (pre) --expr 13Conditional ? : right-to-left Cast ( type ) 14Assignment = += -= *= /= %= >>= <<= &= ^= |= right-to-left Size in bytes sizeof 3 Multiplication * left-to-right 15Comma, left-to-right Division / Highest precedence is Level 1. Lowest precedence is Level 15. Use parentheses () to document or alter the order of evaluation. Modulo % 4 Addition + left-to-right Subtraction -
5 Arithmetic Operators Order of precedence (High → Low): Unary + - * / % + - OperatorDescriptionExample (Base 10) Unary + Denotes positive value+3 Unary - Denotes negative value Addition is 16 - Subtraction is 8 * Multiplication12 * 4 is 48 / Division12 / 4 is 3 % Modulus (remainder)12 % 4 is 0, 12 % 5 is 2
6 C lacks a built-in “to-the-power-of” operator, AKA exponentiation Include header file math.h. Use the math library function called pow () Syntax: pow( base, exponent ) // returns base exponent Example: 2.2+(3.5) 3 → * 3.5 * 3.5 -or pow( 3.5, 3 ) Example: y (x+1)/2.8 → pow( y, ( x + 1 ) / 2.8 )
7 Relational and Logical Operators Order of precedence (High → Low): ! >= == != && || TypeOperatorDescription Relational (comparing values) < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to != Not equal to Logical (Boolean logic) ! Logical negation && Logical AND (short-circuit) || Logical OR (short-circuit)
8 Expressions expression a valid combination of constants, variables, function calls, and operators that produces a proper value Relational and logical operators are often used in logical (AKA boolean) expressions Numeric value of a relational or logical expression is: false if the computed result is 0 true if the computed result is not 0
9 Examples: w = 5 + sin(3.14) / 2.75; → x = 123 – ( 10 * w ); → t1 = w >= 5; → 1 (i.e., true) t3 = x > 0 && w < 5; → 0 (i.e., false) ! (exclamation) is the unary negation operator. A zero operand is converted to 1 A non-zero operand is converted to 0 Example: x = 0; y = 3; !x → 1 !y → 0
10 Pre- Post-Increment, Decrement Operators Increment operator ++ adds 1 to its operand x++ is equivalent to x=x+1 Decrement operator -- subtracts 1 from its operand x-- is equivalent to x=x-1 Increment / decrement modes: Prefix: ++x --x Postfix: x++ x-- OperatorDescriptionExample (x is variable) ++ Increment ++x, x++ -- Decrement --x, x--
11 Example: w = 0; z = 15; w++; → 1 ++w; → 2 z--; → 14 Example: Prefix versus Postfix n = 5; x = n++; Result: n → 6 x → 5 (Get value of n, store in x, increment n ) n = 5; x = ++n; Result: n → 6 x → 6 (Increment n, get value of n, store in x ) ++ and – apply to integer variables
12 Assignment Operators var op= expr is equivalent to var = var op expr where op is +, -, *, / Example: i = i + 2; can be replaced by i += 2; Assignment operators are: += -= * = /= Example: dec -= 2; is equivalent to dec = dec - 2; p *= 5; is equivalent to p = p * 5;
13 Bitwise Operators Bitwise operators act on individual bits. OperatorDescriptionExample (unsigned char) Base 2 Calculation ~ bitwise complement ~x ~128 is 127 & bitwise AND x & y 12 & 5 is 4 | bitwise OR x | y 12 | 5 is 13 ^ bitwise XOR x ^ y 12 ^ 5 is 9 << bitwise shift left x << #of bits 46 << 1 is 92 >> bitwise shift right x >> #of bits 130 >> 2 is → → → 4 12 → → → → → → → → → → → → 127
14 Example: (assume MSB is bit #7 and LSB is bit #0) Read only bit #7 (use mask = ) if SR & 128 equals 128, then bit #7 was 1, otherwise bit #7 was 0 Suppose SR = SR & = Suppose SR = SR & = Set only bit #6 to 0 (use mask = ) CR = CR & 191; Suppose CR = CR & = Suppose CR = CR & = Set only bit #2 to 1 (use mask 4 10 = ) CR = CR | 4; Suppose CR = | = Suppose CR = | =
15 Type Conversion (Casting) “lower” type is promoted to “higher” type before the operation proceeds. The result is of the higher type. If either operand is a long double, convert the other to long double Otherwise, if either operand is double, convert the other to double Otherwise, if either operand is float, convert the other to float Otherwise, convert char and short to int Then, if either operand is long, convert the other to long
16 The cast operator can convert the value of an expression to a different type. Syntax: (type) n Returns the value of n cast to the specified type. The original type of n itself is not altered. Example: char x = 'A'; int y; y = x;/* Automatic cast; watch out */ y = (int) x; /* Explicit cast */
17 Be aware of mixed types when doing arithmetic! Example: float x; int y = 3; x = 1 / 2; x = 1.0 / 2; x = y / 2; x = (float) y / 2; x contains 0.0 x contains 0.5 x contains 1.0 x contains 1.5
18 Memory Access Operators All variables are stored in memory, and each memory location has a unique address. The & operator returns the address of a variable. Example: Suppose x is located at memory address The 32-bit integer number 21 is stored there. OperatorDescriptionExample ( x is a variable) * Indirection *x & Reference (Address of) &x sizeof Size of type (in bytes) sizeof(x) Value of x is 21 Value of &x is 1024 sizeof(x) is 4 AddressContents x