You are on page 1of 40

Operator

Operator:-
It is a symbol that tells the computer to
perform certain mathematical or logical
manipulation.
It is used in programs to manipulate data &
variables.
It acts an connectors.

Three categories:-
1.Unary
2.Binary
3.Ternary
Operator
Different types:-
1. Arithmetic
2. Relation
3. Bitwise
4. Logical
5. Increment & decrement
6. Conditional
7. Assignment
8. Special
Arithmetic Operator

operator meaning

+ Addition
- Subtraction
* Multiplication
/ Divison
% Modulo
Arithmetic Operator
Division operator ‘/’ is applied to an
integer or character, it truncates any
fractional part.
E.g: 7/2=3
Unary minus multiplies its single operand
by -1.
E.g: -3
Modulo division produces the remainder of
an integer division.
E.g: 7%2=1.
It cannot be used on floating point data
Arithmetic Operator
C Arithmetic are of 3 types:
1. Integer Arithmetic.
2. Real Arithmetic.
3. Mixed-Mode Arithmetic.
Arithmetic Operator
1. Integer Arithmetic
When the both operands are integer.
if a=13 & b=2;
Operation Result
a+b 15
a-b 9
a*b 26
a/b 6( decimal
truncated)
a%b 1(remainder)
Arithmetic Operator
During Integer divison :
1.If both are operands are same sign : result is
truncated towards Zero.
2.If one of them is negative : truncation is
machine dependent.
Eg: 6/7 = 0
-6/-7 =0
But -6/7 =0 or -1 (machine dependent)
Arithmetic Operator
During modulo divison:
The sign of the result is always the sign of
the first operand (dividend).
Eg:
-6/5 = -1
-6/-5 = -1
6/-5 = 1
Arithmetic Operator
2. Real Arithmetic
It involve only real operands.
It may either in decimal or exponential
notation.
Its values are rounded to the significant value.
Eg:
6.0/7.0 = 0.857143
1.0/3.0 = 0.333333
-2.0/3.0 = -0.666667
Note: Modulo operator ( %) cannot be used with
real operands
Arithmetic Operator
3. Mixed-mode Arithmetic
One operand is real & other is integer.
Eg:
12/10.0 = 1.2
12/10 = 1
Relational Operator

operator meaning

< is less than


<= is less than or
equal to
> is greater than
>= is greater than
or equal to
== is equal to
!= is not equal to
Realtional Operator
They are >, >=, <, <=, ++, !=
These are the symbols that are used to test the
relationship between two variables or between a
variable and a constant.
An expression containing a relational operator
is termed as Relational expression. Value is 0, if
the result is TRUE, else the value is 1.
Arithmetic operators have a higher priority
over relational operators.
Relational expressions are used in decision
making statements such as if, while, to decide the
action of a running program
Relational Operator
Eg:

Operator Result
6>5 1 (True)
3>9 0 (False)
5!=6 1(True)
9>=19 0(False)
-12>=0 0(False)
Logical Operator
They are &&, ||, !.
An expression which combines two or
more relational expressions is termed
as logical or compound relational
expression.
They are used when we want to test
more than one condition and make
decisions.
Logical Operator

Operator Meaning

&& Logical AND

|| Logical OR

! Logical NOT
Logical Operator

A B A&&B A||B !A
F(0) F(0) F F T
F(0) T(1) F T T
T(1) F(0) F T F
T(1) T(1) T T F
Logical Operator
Eg: a=9 , b=5 & c= 2

Operator Result
(a > b) && (a > c) 1
(a > b) && (a < c) 0
(a > b) || (a < c) 1
(a < b) || (a > c) 1
Bit-wise Operator
They are &, |, ^, <<, >>, ~
They are used for manipulation of data at bit
level.
These operators are used for testing the bits, or
shifting right or left.
These operators may not be applied to float or
double values.
It allows the programmer to interact directly with
the hardware of a particular system through
bitwise operators and expressions
Bitwise Operator
Operator Meaning
& (ampersand) Bitwise AND
| (pipeline) Bitwise OR
^ (caret) Exclusive –
OR(XOR)
~ (Tilde) 1’s Complement
<< Left shifting
>> Right shifting
Bitwise Operator
Eg: a=4 b=3

a= 4 0000 0100
b= 3 0000 0011
a&b=0 0000 0000

a= 4 0000 0100
b= 3 0000 0011
a |b=7 0000 0111
Bitwise Operator

a= 4 0000 0100
b= 3 0000 0011
a^b=7 0000 0111

a = 10 1010
~a=5 0101
Bitwise Operator
Shifting : It changes the contents of input
data at bit level.
It two types :
1.Left shifting
2.Right shifting
Syntax:
<variable> <shift operator> <no of bits to shift>

Note: It cannot be applied to float / double


Operator
Right shifiting :

a=132 1000 0100


a >>1 0100 0010
a >> 2 0010 0001

When it is shifted the right-most end bits are


deleted & left most zeros are added
It is dividing power by 2
Operator

2 shift value Divide by Shift operator


1 2 >>1
2 4 >>2
. .
n 2n >>n

If shifting digit by 2 places to right: dividing by 4


i.e. 2 square.
If 3 places means 8 i.e. 2 cube
Bitwise Operator
1. Left shifting

a=132 1000 0100


a << 1 0000 1000
a << 2 0001 0000

2. If we shift 1 place to left ,the left most 1 digit


lost & 1 zero is added to the right.
3. multiplying by 2
Operator
If shifted by 2 places ,its multiplied by 4 i.e, 2
square

2shift Multiplies Shift


by operator
1 2 <<1
2 4 <<2
3 8 <<3
.. .. ..
n Pow(2,n) <<n
Increment and decrement operator

The increment operator is represented like ++.

This operator is to add 1 to the value of the associated variable.

For example, ++m means m = m + 1.

Two forms:

1. ++m. (Prefix)

2. m++. (Postfix)

Example: m = 10;

n = ++m; // n will get 11 and m will also be 11


Operator
The decrement operator is represented like - -.

This operator will subtract 1 from the value of the associated


variable.

Example: - -m means m = m – 1.

Two forms:

1. --m(Prefix)

2. m– (Postfix)

Example:

  m = 10;
Conditional Operator

A ternary operator pair “?:” is available in C to construct

conditional expressions of the

Form: expr 1 ? expr2 : expr3;

Here, expr1 is evaluated first. If it is nonzero (or true), then expr2 is

evaluated and this becomes the result of the expression. However, if

expr1 is false, expr3 is evaluated and it becomes the value of the

expression.

Ex: a = 10; b = 15;


Assignment Operator
They are used to assign the result of an expression to a variable.

We have the usual operator which is “=”.

The statement “a = 10;” means, the variable a will get a value 10 stored
in it.

Another form of the assignment operator is the shorthand arithmetic


operator.

This takes the form “a += 1”.


Special Operator
They are
1.Comma
2. sizeof
3.Pointer
4.member selection operators.
Special Operator
Comma operator:
1.It can be used to link the related expressions together.
2. Since comma operator has the lowest precedence of all
operators, the parenthesis are necessary.
sizeof operator:
1. It will determine the size of variables storage in bytes.
Eg: n=sizeof(long int);
2. It is an unary operator.
3. This is very useful in writing machine-dependent
programs.
4. It is also called as compile time operator i.e the
necessary information to compute the size of any variable
is known at compile time, irrespective of changes made at
execution time.
Operator
Pointer operators:
1.They are more efficient in handling the data
tables.
2.The address of a variable can be found with the
help of & operator.
3. The use of pointer array to character strings
results in saving of data storage space in
memory.
Member selection Operators:
They are used to select the members of a
structure
Precedence of Arithmetic Operators
The priority or precedence in which the operations of
an arithmetic statement are performed is called
hierarchy of operations.
Precedence is used to determine how an expression
involving more than operator is evaluated.
The operators at the Higher level of precedence are
evaluated first.
The operators of the same precedence are evaluated
either from left to right, or from right to left, depending
on the level. This is known as the associatively property
of an operator.
Parenthesis may be nested and in such cases,
evaluation of the expression will proceed outward from
the innermost set of parenthesis.
Operator

Operator Associatively Rank


() [] Left to Right 1

Unary plus, minus, ++,--, Right to Right 2

! , * (pointer reference), & Left to Right 3


(address), sizeof, (type)

+, - 4
<, <=, >, >= 5
== , != 6
&&, || 7,8
?: , =, *=, /=,%=, +=, -= 9,10
, i.e comma 11
Type Conversions in Expressions
When constants and variables of different types are mixed in an
expression they are converted to the same type. This conversion is
done implicitly by C compiler.
C Compiler will convert all operands to the type of the largest
operand.
All the chars and short ints are converted to ints.
All floats are converted to double.
The final result of an expression is converted to the type of the
variable on the left of
the assignment sign before assigning the value to it.
During the final assignment,
a) float to int causes truncation of the fractional part.
b) double to float causes rounding of digits.
c) long int o tint causes dropping of the excess higher order bits.
Type Casting
Type cast provides a method for converting a
variable to a particular type.

It forces an expression to be of specific data


type by using a construct called cast.

Syntax: (type) expression.

Eg; If x is original declared as a type int, then


to convert it to a type float. Y= (float) x
Control Statements_

They are mainly classified into 3 categories:

1.Conditional expressions.

2.Loop statements.

3.Breaking Control Statements.


Control Statements_
1. Conditional expressions.
1. if statement
2. If-else statement
3. Nested if-else statement
4. if-else-if ladder
5. switch statement
Operator

You might also like