A Crash Course in C - copyright 1995 by Anand Mehta - Basic Operators
Some basic operators are
- expression operators
- unary operators
- !, ~, ++, --, +, -
- !: negation
- !(0) ==> 1
- !(any non-zero value) ==> 0>
- ++/--: increment/decrement by 1
- prefix: increments the variable and gives the new value as the result
- postfix: gives the old value as the result and then increments the variable
- arithmetic operators
- *, / , %
- % is called modulus division (remainder)
- +, -
- bitwise shift operators
- comparison operators
- the logical operators return a 1 (true) or 0 (false)
- for any conditional test, any non-zero value is true and a 0 is false
- <,>, <=, >=
- ==, !=
- == is the equality comparision (not assignment =)
- A common error is using the assignment operator = when the logical operator == is required, e.g. (x = 2) instead of (x == 2); both are valid expressions, so the compiler will not indicate an error.
- bitwise operators
- & - and
- ^ - exclusive or
- | - or
- logical operators
- &&, ||
- logical operators are evaluated until an expression is known to be true or false
- conditional, compact if-else as an expression instead of a statement
- assignment operators
- =, +=, -=, *=, /=, %= !
- op= assignment,
E1 op= E2 <==> E1 = E1 op E2, with E1 evaluated once
- for example,
x+=2 <==>x=x+2
- , (comma)
- combines separate expressions into one
- evaluates them from left to right
- the value of the whole expression is the value of the right most sub-expression
Next slide