Presentation is loading. Please wait.

Presentation is loading. Please wait.

ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed.

Similar presentations


Presentation on theme: "ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed."— Presentation transcript:

1 ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed by Professor Phillip Wong @ PSU ECE

2 Syllabus Operator Precedence and Associativity Arithmetic Operators Relational and Logical Operators Increment and Decrement Operators Assignment Operators Type Conversion (Casting) Memory Access Operators

3 2 Operator Precedence and Associativity Operators perform very 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 Associativity Rules → specifies which operator is evaluated first when operators of the same precedence are adjacent

4 3 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 alter the order of evaluation. Modulo % 4 Addition + left-to-right Subtraction -

5 4 Arithmetic Operators The C language does not support element-by-element operators (e.g., like the.* or.^ operators in MATLAB). OperatorDescriptionExample (Base 10) Unary + Denotes positive value+3 Unary - Denotes negative value -3-3 * Multiplication12 * 4 is 48 / Division12 / 4 is 3 % Modulus (remainder)12 % 4 is 0, 12 % 5 is 2 + Addition12 + 4 is 16 - Subtraction12 - 4 is 8

6 5 C lacks a built-in “raise to a power” operator. Include the header file. Use the math library function called pow. Syntax: pow(base, exponent) returns base exponent Example: 2.2+(3.5) 3 → 2.2 + 3.5*3.5*3.5 -or- 2.2 + pow(3.5, 3) Example: y (x+1)/2.8 → pow(y,(x+1)/2.8)

7 6 Note: Do not use pow() solely to express a numeric constant in base 10 scientific notation. Example: 1.25  10 3 should be written as 1.25e3 in C code. Although 1.25*pow(10,3) works, it is inefficient. Example: 5.3  10 -5 should be written as 5.3e-5 in C code. Example: 2  3 8 is not base 10, so the e notation cannot be used. Instead, 2*pow(3,8) is correct usage here. Example: 6.5  10 X is base 10 but not a constant. Use 6.5*pow(10,X).

8 7 Relational and Logical Operators TypeOperatorDescription Relational (comparing values) < Less than > Greater than <= Less than or equal to >= Greater than or equal to == Equal to != Not equal to Logical (Boolean logic) ! Logical negation && Logical AND (short-circuit) || Logical OR (short-circuit)

9 8 expression  a valid combination of constants, variables, function calls, and operators that, when evaluated, produces a value. The numeric value of a relational or logical expression is: 1 if the expression is true 0 if the expression is false

10 9 Example: w = 5+sin(3.14) / 2.75; → 5.0006 x = 123 – (10*w); → 72.9942 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

11 10 Increment and Decrement Operators Example: w = 0; z = 15; w++; → 1 ++w; → 2 z--; → 14 OperatorDescriptionExample (x is a variable) ++ Increment (add 1 to operand) ++x or x++ equivalent to x=x+1 -- Decrement (subtract 1 from operand) --x or x-- equivalent to x=x-1

12 11 Example: Prefix ( ++u, --u ) versus Postfix ( u++, u-- ) n = 5; x = ++n; Result: n → 6 x → 6 (Increment n, get value of n, store in x ) n = 5; x = n++; Result: n → 6 x → 5 (Get value of n, store in x, increment n )

13 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;

14 13 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

15 14 The cast operator explicitly converts the type 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 y = (int) x; // Explicit cast

16 15 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

17 16 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 1024. 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 102421


Download ppt "ECE 103 Engineering Programming Chapter 4 Operators Herbert G. Mayer, PSU Status 6/10/2016 Initial content copied verbatim from ECE 103 material developed."

Similar presentations


Ads by Google