COMPUTATIONAL THINKING FOR
STRUCTURED DESIGN
Prof. RITU JAIN, Assistant Professor Computer
Science & Engineering
Operators in C
Operators are the foundation of
programming
language.
C language provide different kind of
operators. Different types of
1. Arithmetic
operators in C are :
Operators 2.
operators
Relational operators
in C 3. Logical operators
4. Assignment operators
5. Increment and decrement
operators
6. Conditional operators
7. Bitwise operators
8. Special operators
Arithmetic operators are binary operators
usedare
which to perform arithmetic
operation.
These are :
+ - add
subtract
Arithmet * multiply
/ divide( divisor must be
ic % non zero ) modulo(gives
Operato div)
remainder after
The parenthesis() are used to clarify
rs operations. The operators + and - can
complex
be used as unary plus and unary minus
arithmetic operators also. The unary –
negates the sign of it’s operand .
Note : C language has no
operator for exponentiation.
The function pow(x,y) which exists in
header file math.h of standard
library and returns Xy
Following are some examples of
arithmetic operators :
x+y, x-y, x*y, x/y, x%y, -x*y
Here x and y are operands that can take
any value but % operator cannot be
used on floating point data type.
An expression consisting of numerical
values(either any number, variable or
even some function call) joined together
by arithmetic operators is known as an
arithmetic expression. For example ,
consider the following expression :
Arithmeti (x-y)*(x+y)/5
Here x,y and 5 are operands and the
c symbols -,*,+,/ are
operators.
Expressio The precedence of operators for the
ns expression evaluation has been given by
using parenthesis which will over rule
the operators precedence. If x=25 and
y=15,then the value of this expression
will be 80.
Consider the following
expression : 3*((a
%4)*(5+(b-2)/(c+3)))
Where a,b,c are integer variables
and if a,b ,c have values 9
Arithmeti ,14 ,16 respectively then above
expression would
3 * ((9%4) * (5be
+ evaluated
(14 – 2) / as
c (6 +3 )))
Expressio = 3 * ( 1 * (5 + (12 / 9) ) )
ns = 3 * ( 1 * (5 + 1))
=3 * ( 1 * 6 )
=3 * 6
= 18
In C, the arithmetic operators have
the priority as shown below:
First priority * /
% Second priority
Arithmeti
+ -
c Thirdsequence
The priority of=operations in
Operator evaluating an arithmetic
expression is also known as
s hierarchy of operations. This is
Preceden necessary to avoid any doubt
while evaluating an expression.
ce The following precedence rules are
followed in expression evaluation :
(i) All the subexpressions within the
parentheses are evaluated first.
Nested parenthesized
subexpressions are evaluated
inside-out, with the innermost
Arithmeti (ii)expression
Operators inbeing
the first
same tosub
be
evaluated.
c expression are evaluated as
given : *, / ,% next.
+, - performed perform
Any first
Operator referenced
function (i.e.,invoked)in the
s expression gets the highest
precedence over all the
Preceden arithmetic operators.
ce (iii)Operators in the same expression
with the same priority are evaluated
from left to right.
For example : consider the following
expression for checking the operators
precedence.
15 * 7 / ( 2 – 3 * 5 / 7 + 4 ) –
Arithmeti 7*9%4
= 15 * 7 / (2 – 15 / 7 + 4 ) – 7
c *9%4
Operator = 15 * 7 / (2 – 2 + 4) – 7 * 9
%4
s = 15 * 7 / 4 – 7 * 9 % 4
Preceden = 105 / 4 - 63 % 4
ce = 26 – 3
= 23
C language has two useful operators called
increment(++)
decrement (--)and
that operate on integer data only.
The increment (++) operator increments the
operand by 1, while the decrement operator (--)
decrements the operand by 1.
Increme There are two type of increment (++) operator : pre
and post
nt and ,Similarly There are two type of decrement (--)
operator : pre and post, for example :
Decreme int a , b;
a = 10;
nt b = a++ ;
Operator printf(“ %d %d “, a, b);
11 10 . First a is assigned
OUTPUT
to b and then a is incremented by 1
i.e.,post-increment takes place
If we int a, b ;
have : a = 20;
b = ++a;
printf(“%d %d”,
Increme OUTPUT : a, 21
b); 21. first a is
incremented by 1 and then assignment
nt and take place i.e., pre-increment of a.
now, consider the example for (--)
operator :
Decreme int
nt a,b;
a=10;
Operator OUTPUT :9b=10. first a is assigned to b
a--; then a is
decremented by 1. i.e.,post decrement
printf(
“%d takes place
%d”,
a , b)
If we have :
int i, j ;
I = 20;
j = --i;
printf(“%d %d”,
Decreme OUTPUT : i, 19j); 19. first i is
nt decremented by 1 and then assignment
take place i.e., pre-decrement of i.
Operator
Note : on some compilers a space is
required on both sides of ++i or i++ ,
i-- or --i
These are used to compare two
variables or constants and return
true or false . C has the following
relational operators :
OPERATO MEANING
R Equals
Relation == Not
!= Equals
al <
> Less than
Operato < Greater
= than Less
rs equals Greater
than or
> than or
=
equals
In C, we can have simple conditions
(single) or compound conditions(two
or more). The logical operators are
used to combine conditions. The
notations
Operatfor these Notation
operatorsinis given
below :
or C
Logical
NOT !
Operato &&
AND
rs OR
The notation ||
for the operator OR is given
by two broken lines. These follow the
same precedence as in other
language. NOT(!) is evaluated before
AND(&&) which is evaluatedbefore
OR(||).
&& : returns true when all the condition
under the consideration are true
and returns false when anyone or
|| : morereturns
than one condition
true when oneis false.
or more
than one condition under the
Logical consideration are true and returns
Operato false when all the conditions are
rs ! : false
NOT operator is used to
complement the condition under
the consideration
Returns true when condition is false and
returns false when condition is true
Precedence of Relational Operators
and Logical Operators
Each operator in C has a precedence of its own. It
helps in evaluation of an expression. Higher the
precedence of the operator, earlier it operates. The
operators having same precedence are evaluated
either from left to right or from right to left,
depending on the level, known as the associativity
of the operator.
! , < , <= , > , >=, ==, !=, ==, !=, &&, ||
The Conditional
Operator
This operator ? And : together forms a ternary operator called as
the conditional
operator.
Synta (test-expression) ? T-expr :
x: F-expr ;
Let us see example
program :
#include<stdi
o.h>
main
()
{
int n;
The
clrscr();
condition printf)(“Enter value
al of n”);
operator scanf(“%d”,&n);
(n%2==0)? Printf(“n
is even”):printf(“n is
not even”);
getch();
}
These are used to perform bitwise operations
such as testing the bits, shifting the bits to left
to right, one’s complement of bits etc. these
operations can be applied only on int and char
Various bitwise
data types butoperators in C and double data
not on float
language
types. are :
Bitwise ~ Bitwise (1’s)
complement)
Operato <
< shift left
rs > shift right
> bitwise
& AND
^ bitwise
| XOR(Exclusive OR)
bitwise OR
C • Comma Operator
provide • sizeof operator
• Address operator
s the • Dereferencing
Specia followin operator
l g • Dot operator
special • Member
Operat operato selection
or rs operator
• Pointer
:
The comma operator (,) has the lowest precedence.
The comma operator is mainly used in for statement.
For example
:
int i , j;
The for(i=1 , j=400 ; i<=10 ; ++I , j/=2)
Comma printf(“%d\n”, i+j ) ;
The initial value of i is 1 and that of j
the value of i is 400 and every time is
Operator incremented
2 by 1of
after execution and
thethat
bodyof of the for jloop
is divided
.
by
The distinct expression on either side of the comma
operator are evaluated from left to right.
The associativity of comma operator is from left to
right .
It is a unary operator which provides the size , in
bytes, of the given operand. The syntax of sizeof
operator is :
sizeof(opera
nd)
The
Here the operand is a built in or user defined data
sizeof type or variable. The sizeof operator always
Operato precedes its operand.
For example, sizeof (float) returns the value 4 .
r The sizeof operator mainly used in dynamic memory
allocation for calculating the number of bytes
used by some user defined data type.
Precedence of operators among
themselves and across all the
sets of operators.
The TURBO C operators are divided into
the following 16 categories : these are
ordered from the highest precedence to
the lowest precedence. The operation
within each category have equal
precedence.
Category Operator What it does ?
Precedenc 1. Highest precedence ()
[]
Function call
Array subscript
e of -> C indirect component selector
operators
2.Unary ! NOT
~ Bitwise(1’s) component
among +
-
Unary plus
Unary minus
themselve 3.Member acces .* Dereference
s and 4.Multiplication
->*
*
Dereference
Multiply
across all /
%
Divide
Remainder (Modulus)
the sets of
operators.
Category Operator What it does ?
5.Additive + Binary plus
- Binary minus Preceden
6.Shift <<
>>
Shift left
Shift right
ce of
7.Relational < Less than operators
<= Less than or equal to among
>
>=
Greater than
Greater than equal to
themselv
8.Equality == Equal to es and
!= Not equal to across all
9.Bitwise AND
10.Bitwise XOR
&
^
Bitwise AND
Bitwise XOR
the sets
11.Bitwise OR | Bitwise OR of
operators
.
Category Operator What it does ?
12.Logical AND && Logical AND
13.Logical OR || Logical OR Preceden
14.Conditional ?: (exp?x:y)
ce of
15.Assignment = Simple assignment
operators
among
*= Assign product
/= Assign quotient
%= Assign remainder (modulus)
themselv
+= Assign sum es and
-= Assign difference across all
&= Assign bitwise AND the sets
^= Assign bitwise XOR
of
|= Assign bitwise OR
operators
<<= Assign left shift
>>= Assign right shift
.
The Associativity of
Operators
In C , the operators having the
equal precedence are evaluated
either from left to right or from
right to left, depending on the
level. It is known as associativity
property of the operator.
Category Operator Associativity
1.Highest () Left to Right
precedence
[]
->
::
Associativ 2.Unary
.
! Right to left
ity of the ~
Operator +
-
++
--
&
*
sizeof
Category Operator Associativity
3.Member access .* Left to Right
->*
4.Multiplication * Left to right
/
%
Associativ
5.Additive + Left to Right
- ity of the
6.Shift << Left to Right Operator
>>
7.Relational < Left to Right
<=
>
>=
Category Operator Associativity
8.Equality == Left to Right
!=
9.Bitwise AND & Left to right
10.Bitwise XOR ^ Left to Right
11.Bitwise OR | Left to Right Associativ
12.Logical AND && Left to Right
13.Logical OR || Left to Right
ity of the
14.Conditional ?: Right to Left Operator
15.Assignment = Right to Left
*=
/=
%=
+=
-=
Category Operator Associativity
&= Right to Left
^=
|=
<<=
>>=
16.Comma . Left to Right
Associativity of the
Operator
33