Operators and Expressions
Operators and Expressions
Expressions
Combination of Operators and Operands
Operators
Example
2*y+5
Operands
Operators
4 Types
Arithmetic
Logical
Relational
Bitwise
Arithmetic Expressions
Mathematical expressions can be expressed in C using
arithmetic operators
Examples
++i % 7
5 + (c = 3 + 8)
a * (b + c/d)22
Relational Operators
Used to
Test the relationship between two variables, or between a variable and a
constant
Relational Operators
Logical Operators
Logical operators are symbols that are used to combine or
negate expressions containing relational operators
True table
Variable
Expression
A && B
A || B
!A
OR
( NUM1 | NUM2 )
NOT
( ~ NUM1)
XOR
Returns 1 if either of the bits in an
( NUM1 ^ NUM2) operand is 1 but not both
True table
bit
Expression
A&B
A|B
~A
A^B
Examples
Example
10 & 15 1010 & 11111010 10
10 | 15 1010 | 11111111 15
10 ^ 15 1010 ^ 11110101 5
~ 10 ~1010 1011 -11
Precedence Of Operators
Operator Class
Operators
Associativity
Unary
- ++ --
Right to Left
Binary
Left to Right
Binary
*/%
Left to Right
Binary
+-
Left to Right
Binary
Right to Left
Precedence Of Operators-2
Operator
NOT
AND
OR
Precedence among
Operators-1
When an equation uses more than one type of operator then
the order of precedence has to be established with the
different types of operators
Precedence
1
2
3
Type of Operator
Arithmetic
Comparison
Logical
Precedence among
Operators-2
Consider the following example:
2*3+4/2 > 3 AND 3<5 OR 10<9
[2*3+4/2] > 3 AND 3<5 OR 10<9
[[2*3]+[4/2]] > 3 AND 3<5 OR 10<9
[6+2] >3 AND 3<5 OR 10<9
[8 >3] AND [3<5] OR [10<9]
True AND True OR False
[True AND True] OR False
True OR False
True
Changing Precedence-1
Parenthesis ( ) has the highest level of precedence
The precedence of operators can be modified using
parenthesis ( )
Operator of lower precedence with parenthesis
assume highest precedence and gets executed first
In case of nested Parenthesis ( ( ( ) ) ) the inner
most parenthesis gets evaluated first
An expression consisting of many set of parenthesis
gets processed from left to right
Changing Precedence-2
Consider the following example:
5+9*3^2-4
5+9*3^2-4
5+9*3^2-4
5+9*3^2-4
5+9*3^2-4
5+9*3^2-4
5+9*3^2-4
5+9*3^2-4
Changing Precedence-4
5+9*9-4>10 AND True
5+81-4>10 AND True
86-4>10 AND True
82>10 AND True
True AND True
True