Operators and Expressions
Operators and Expressions
Operators and Expressions
1. Arithmetic operators.
2. Relational operators.
3. Logical operators.
4. Assignment operators.
5. Increment and decrement operators.
6. Conditional operators.
7. Bitwise operators.
8. Special operators.
Operators offer the following properties
A) Precedence
B) Associativity
It is one of the most important properties of the operator.
Precedence refers to the priority alloted to the operator for
processing.
An expression contains many operators then the operations are
carried out according to the priority of the operators
predetermined.
Higher priority operator assessed first.
Example
*/% ---- Highest precedence
+- ---- Lowest precedence
Example
8 + 9 * 2 – 10
The operator * is the highest priority. Hence
multiplication operations is performed First.
=8 + 18 – 10
+ and – are having priority. In this situation,
the left most operation is solved first.
=26 – 10
=16
When an expression having operators with
equal precedence then the associativity
property decides which operation is
performed first.
Associavity means the direction of execution.
Classified as two types.
1) Left to Right
2) Right to Left
In this type expression evaluation starts from left to
right direction.
Example
12 * 4 / 8 % 2
In this types of` expression, all operators having
same precedence so associativity rule is followed.
=48 / 8 % 2
=6% 2
=0
In this type expression evaluation starts from left to
right direction
Example
X=8 + 5 % 2
=8+1
=9
Example
1 ) 5 * 4 + 8 / 2;
2 ) ( 8 / ( 2 * ( 2 * 2 ) ) );
Note:
If there are some sets of parenthesis in the
expression, the inner most parenthesis will be
solved first followed by the second and so on.
Precedence and associativity of operators
Precedence Operator Operation Associativity
level
1 () Function call Left to Right
[] Array subscript Left to Right
. Dot Left to Right
-> Arrow Left to Right
2 ! Logical NOT Right to Left
~ One’s complement Right to Left
- Unary minus (negation) Right to Left
++ Increment Right to Left
-- Decrement Right to Left
& Address of Right to Left
* Indirection Right to Left
(data_type) Cast operator Right to Left
sizeof Size of Right to Left