Operators
&
Expressions
Operator
An operator is a symbol used to indicate a specific
operation on variables in a program.
Example : symbol “ + “ is an add operator that
adds two data items called operands.
Expression
An expression is a combination of operands ( constants,
variables, numbers) connected by operators and
parenthesis.
Example :
A + B
operator
operand
Types of expression
Arithmetic expression : An expression that involves
arithmetic operators. The computed result of this
expression is a numeric value.
Logical or Boolean expression : An expression that
involves relational and/ or logical operators. The
result of this expression is a logical value i.e either
1 (TRUE) or 0 (FALSE)
Operators
C language is very rich in operators.
Four main classes of operators :
» Arithmetic
» Relational
» Logical
» Bit wise
Assignment operator
It is used to assign variable a value:
variable_name = expression;
lvalue : In compiler lvalue error messages means that an
object on left hand side of assignment operator is missing.
rvalue : In compiler rvalue error messages means that
expression on right hand side of assignment operator is
erroneous.
Two cases of assignment
Multiple assignment:
int j=k=m=0;
Compound assignment:
j= j+10; this expression can be written as
j + = 10;
similarly
m= m-100; is equivalent to m - = 100;
Arithmetic operators
Following operators are used for arithmetic operations on
all built in data types :
+ (unary plus)
- (unary minus)
+ (addition)
- (subtraction)
* (multiplication)
/ (division or quotient)
% (modulus or remainder)
-- (decrement)
++ (increment)
Binary arithmetic operators
A binary operator requires two operands to work
with.
Addition, subtraction, multiplication, division, and
modulus or remainder operator falls in this category.
The evaluation of binary operator is LEFT
associative that is in an expression operators of
same precedence are evaluated from left to right.
Order of evaluation
For a complex expression it becomes difficult to
make out as to in what order the evaluation of sub
expression would take place.
In such case we check out the precedence and
associativity of operators in the expression.
Precedence
Defines the order in which an expression is evaluated
operators Precedence
*,/,% High and are on same level
+,- Lower and are on same level
Example
int x; output:
x= 7 + 3 * 5; x = 22
int x; output:
x= ( 7 + 3) * 5; x = 50
int x; output:
x= 7 / 3 * 5; x = 10
Modulus operator ( % )
This operator has same priority as that of
multiplication and division
a % b = output remainder after performing a / b
Example:
7 % 10 = 7
7%1 = 0
7%2 = 1
7%7 =0
Exercise
int y;
y = 10 % 4 * 3; Output:
6
int y; Output::
y = 3 * 10 % 4; 2
Important
Modulus operator ( % ) : It produces remainder of an integer
division. This operator can not be used with floating point
numbers.
int main( ){
float f_1=3.2, f_2=1.1, f_3 ;
f_3 = f_1 % f_2;
printf(“ %f”, f_3);
return 0; }
Output: error at line 3
illegal use of floating point
Invalid arithmetic expressions
a * + b : Invalid as two operators can not be used in continuation.
a( b * c ) : Invalid as there is no operator between a and b.
Unary arithmetic operators
A unary operator requires only one operand or data item.
Unary arithmetic operators are:
» Unary minus ( - )
» Increment ( + +)
» decrement ( - - )
Unary operators are RIGHT associative that is they are
evaluated from right to left when operators of same
precedence are encountered in an expression.
Unary minus ( - )
It is written before a numeric value, variable or expression
Its effect is NEGATION of the operand to which it is applied.
Example:
- 57 - 2.933 -x
-( a * b) 8 * ( - ( a+b))
Increment operator ( ++ )
The increment ( ++ ) operator adds 1 to its
operand.
n = n +1 ; => ++ n ;
Postfix Increment ( n ++) : It increments the value
of n after its value is used.
Prefix Increment ( ++ n) : It increments the value
of n before it is used.
Example 1
1. x = n++ ;
2. x = ++n ;
Where n = 5;
case 1: It sets the value of x to 5 and then
increments n to 6.
x = 5 and n= 6
case 2: It increments the value of n and then sets
the value of x to 6.
x = 6 and n =6
Example
sum=x++; sum = ++x;
Sum = x; x=x+1;
x=x+1; Sum=x;
Example 2
int i =10, net;
Output:
net = ++i * 5;
i = 11
printf(“ \n i = %d”, i);
net = 55
printf(“\n net = %d”,net);
int i =10, net;
net = i++ * 5; Output:
printf(“ \n i = %d”, i); i = 11
printf(“\n net = %d”,net); net = 50
Decrement operator
The decrement ( - - ) operator subtracts 1 from its
operand.
j=j-1; => -- j;
Postfix decrement ( y - -) : In this case value of
operand is fetched before subtracting 1 from it.
Prefix decrement ( - - y) : In this case value of
operand is fetched after subtracting 1 from it.
Example
sum=x--; sum = --x;
sum = x; x=x-1;
x=x-1; sum=x;
Precedence of Arithmetic operators
Highest : ++ --
- (unary minus)
* / %
Lowest + -
Operators on same level of precedence are
evaluated by the complier from left to right.
Exercise
int x= 34.9;
printf ( “\n\t ++x=%d and x++ = %d”,++ x, x++);
++x = 36 and x++ = 34
Relational & Logical operators
A relational operator is used to compare two values and
the result of such operation is always logical either TRUE
( 1 ) or FALSE ( 0 ).
< less than x<y
> greater than x>y
<= less than or equal to x <= y
>= greater than or equal to x >= y
== is equal to x==y
!= is not equal to x != y
Exercise
Suppose that i, j, and k are integer variables
whose values are 1, 2 and 3, respectively.
Expression Value Interpretation
i<j 1 true
(i + j) > = k 1 true
(j + k) > (i + 5) 0 false
k!=3 0 false
j==2 1 true
Logical operator
A logical operator is used to connect two relational expressions or logical
expressions.
The result of logical expressions is always an integer value either TRUE ( 1 ) or
FALSE( 0 ).(67 > 5) returns 1
(67 = = 5) returns 0
&& Logical AND x && y
|| Logical OR x || y
! Logical NOT !x
Logic AND
The output of AND operation is TRUE if BOTH
the operands are true.
Example: ( 8 < 7 ) && ( 6 > 7)
0 && 0
False ( 0 )
Example 2
(8 > 7) && (6 < 7)
1 && 1
True ( 1 )
Logical OR
The result of a logical or operation will be true if
either operand is true or if both operands are true.
(8 < 7) | | (6 > 7) is false
(8 > 7) | | (6 > 7) is true
(8 > 7) | | (6 < 7) is true
Logical NOT
The Logical NOT ( ! ) is a unary operator. It negates the
value of the logical expression or operand.
If value of X = 0 !X=?
!X=1
! ( 5 < 6 ) || ( 7 > 7 ) = ???
! ( 1) || ( 0) = ! 1 = 0 -> false
! ( 5 > 3) = ??
-> 0 -> false
!(34 >= 765) = ??
-> 1 -> True.
Exercise
x = 10 and y = 25
( x > = 10 ) && ( x < 20 ) True
( x >= 10) && ( y < = 15) False
( x = = 10 ) && ( y > 20 ) True
True
( x==10) || ( y < 20)
( x ==10) &&( ! ( y < 20) ) True
Precedence & Associativity
highest
!(logical NOT) ++ -- sizeof( ) Right to left
* (multiplication) / ( division) % Left to right P
( modulus) R
+ - (binary ) Left to right E
< <= > >= Left to right C
E
==(equal to) !=( Not equal to ) Left to right D
&&(AND) Left to right E
|| (OR) Left to right N
C
? : (conditional ) Right to left
E
= += - = *= /= %= Right to left
, Left to right
lowest
Exercise
Suppose that
j = 7, an integer variable
f = 5.5, a float variable
c = ‘w’
Interpret the value of the following expressions:
( j >= 6) && (c = = ‘w’) 1
( j >= 6) | | ( c = = ‘w’) 1
(f < 11) && ( j > 100) 0
(c ! = ‘p’) | | ((j + f) <= 10) 1
f>5 1
!(f > 5) 0
j<=3 0
!( j <= 3) 1
j > (f +1) 1
!( j > (f +1)) 0
Suppose that
j = 7, an integer variable
f = 5.5, a float variable
c = ‘w’
Interpret the value of the following expressions:
j + f <= 10 0
j >= 6 && c = = ‘w’ 1
f < 11 && j > 100 0
!0 && 0 | | 0 0
!(0 && 0) | | 0 1