Professional Documents
Culture Documents
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
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 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>
Two forms:
1. ++m. (Prefix)
2. m++. (Postfix)
Example: m = 10;
Example: - -m means m = m – 1.
Two forms:
1. --m(Prefix)
2. m– (Postfix)
Example:
m = 10;
Conditional Operator
expression.
The statement “a = 10;” means, the variable a will get a value 10 stored
in it.
+, - 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.
1.Conditional expressions.
2.Loop statements.