Operators And Expressions Operators Arithmetic Operators Relational and Logical Operators Special Operators Arithmetic Operators Operator Action Subtraction, also unary minus + Addition * Multiplication / Division % Modulus -- Decrement ++ Increment 3 Precedence and Associativity Arithmetic Operators: High ++ -- - (unary minus) * / % Low + - - a * b c ((- a) * b) c 4 Expressions inside parentheses are evaluated first. Increment & Decrement Operators Provide a concise notation for incrementing or decrementing a variable by. * ( - 3) Are unary operators. Operators on the same level of precedence are evaluated from left to right. (Associativity). + + 3 + 4 5 ((( + ) + 3) + 4) 5 5 ++x or x++ --x or x-- Can be applied to variables but not to constants or ordinary expressions. ++i; legal cnt--; legal 777++; illegal ++(a * b -); illegal 6
May either prefix or postfix the operand. Increment Postfix: i++; Expression value is the current value (Before you increment) then it increments. use - then increment Prefix ++x; or Postfix x++; x = x + ; Increment Prefix: ++i; Expression value is the value After you increment. increment - then use ++ & -- both cause a value of a variable to change in memory. ( Have a side effect). 7 Decrement Postfix: i--; use - then decrement Decrement Prefix: --i; decrement - then use 8 Examples x =0; y = ++x; y x =0; y = x++; y 0 l = 4; n = 3; m = ; int i = 3, j =, k; x = l * n + m++; 4 x i++; i 4 j = ++i; j 5 i 5 k = i++; k 5 i 6 k = (--j+3) k 7 j 4 After the assignment to x. 3 m 9 0 int a, b, c=0; a = ++c; b = c++; a =? b =? c=? int b =, d = 4; 7--b*++d 7-((-b)*(++d))? int j =, k = 3, m = 4; int a,b; a = ; b = ; printf ( a+++b = %d/n, a+++b); a = ; b = ; printf ( a++ +b = %d/n, a++ +b); a = ; b = ; printf ( a+ ++b =% d/n, a+ ++b); j*=k=m+5 j=(j*(k=(m+5)))?
Relational and Logical Operators Relational Operators: Relational refer to the relationship that value can have with one another. Logical refers to the ways these relationships can be connected. > Greater than >= Greater than or equal < Less than <= Less than or equal = = Equal!= Not equal True is any value other than zero. False is zero. 3 Logical Operators: && AND OR! NOT 4 The truth table for the logical operators. True(), False(0). p q p&&q p q!p 0 0 0 0 0 0 0 0 0 0 Precedence and Associativity High! > >= < <= = =!= && Low!0&&0 0 ((!0)&&0) 0 FALSE 5 6 int x; x = 00; printf(''%d", x>0);? Examples!A is false (0) if A s value is:. is true () if A s value is:. Both are lower in precedence than the arithmetic operators.!!5! (!5)! (0) 0 > + 0 > ( + ) FALSE 0 Associativity: left to right. 5 && 3? 7 8 3
int i, j = ; j i j = j && (i = = 3); ( ) not needed j = j && (i = ); ( ) needed ) (i=) i ) (i = = 3) false 0 ) && j && true && true ) && j && 0 0 3) = j 0 3) = j 9 0 j i j = j (i/); 0 ) (i/) (/) ( ) not needed j i j =!j && (i = i + ); ) i + 3 ) = i 3 ) j true 3)!!j! 0 3) = j 4) && 0 && 3 5) = j 0 The Comma Operator Lowest precedence of all the operators. Causes a sequence of operations, do this and this and this. Is a binary operator. expression_, expression_ Associates left to right. expression_ is evaluated first expression_ is evaluated second The Comma Expression as a whole has the value and type of expression_. int i = ; j = 4; j = i++, i - j; * i 3 * j (3-4) higher precedence than, operator x = (y=3, y+); x 4 ( ) needed 3 4 4
It allows multiple initializations and multiple processing of indices. for (sum=0, i=; i<=n; ++i) sum += i; e.g. int i; i = 0; for ( ; i < 0; putchar ( a + i), i++); will output? 3rd expression in the for statement is a comma expression. Comma Operators can be useful in control statements though many C advocates discourage their use. 5 putchar is called,executed.then i is increased. Most commas in a program DO NOT represent comma operators. see text - pg. 99 6 The ( ) and [ ] Operators Parentheses are operators that increase the precedence of the operations inside them. Square brackets perform array indexing (See Chapter 9 for a discussion of array.) int main(void) { char s[80]; s[3] = 'X'; printf(''%c", s[3]); return 0; } 7 The Conditional Operator? Ternary operator. A powerful and convenient operator that replaces certain statements of the if-thenelse form. Exp? Exp: Exp3 Stands for: if Exp then Exp else Exp3 8 Examples x = 0; y = x>9? 00 : 00; x = 0; if(x>9) y = 00; else y = 00; int i, h, j = ; i = (j==)? :3; k = (i>j)? i:j; ( ) not needed i get k get max of I or j 9 30 5
This statement does what? c = (c > = a && c < = z )? c - ( z - Z ):c; IF True - have a Lower case letter in the variable C. Exp : c - ( z - Z ) will give Capital Letter of whatever is in C. e.g. a - ( z - Z ) 97 - ( 90) = 65 which is A. Expressions vs. Statements An expression in C is any valid combination of operators, constants, functions and variables. A statement is a valid expression followed by a semicolon. Func(); A function call as a statement. Y = X + Y; An assignment statement. B + Func(); Valid? IF False Capital Letter and leaves it alone. 3 3 Null Statement: ; [A semi-colon alone by itself]. Can be useful in loops & conditional statements. The body of a loop could be empty. Scanf( %d, &x); While(printf( %d,x), scanf( %d,&x)==) Spacing and Parentheses x=0/y-(7/x); x = 0 / y - (7/x); Redundant or additional parentheses do not cause errors or slow down the execution of an expression. x = y/3-34*temp+7; x = (y/3) - (34*temp) + 7; ; 33 34 6