0% found this document useful (0 votes)
27 views

06 Operators

Operators in C can be classified into different categories based on their functionality. Binary operators require two operands, unary operators require one operand, and ternary operators require three operands. Common operator categories include arithmetic, relational, logical, assignment, increment/decrement, and bitwise. Each operator performs a specific operation on operands, such as addition, comparison, logical operations, assignment of values, and bit manipulation.

Uploaded by

Vamsi Lankisetty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views

06 Operators

Operators in C can be classified into different categories based on their functionality. Binary operators require two operands, unary operators require one operand, and ternary operators require three operands. Common operator categories include arithmetic, relational, logical, assignment, increment/decrement, and bitwise. Each operator performs a specific operation on operands, such as addition, comparison, logical operations, assignment of values, and bit manipulation.

Uploaded by

Vamsi Lankisetty
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

Operators

Session 6
Terms To Remember

• Operator is a symbol that intercepts a task


• Operation is a task on a given set of data items
• Operand is the data item participating in an
operation
• Expression is a statement formed using oprands
and operators.
Operators

Expression

Operands
Operators in C

Operators

Binary Operators Unary Ternary Pair Bitwise

Arithmetic Malicious

Increment /
Assignment
decrement

Relational Unary Minus

Logical
Definitions Operator Classifications

• Binary Operators are those that need 2


operands to operate.
• Unary Operators are those that need only
one operand to operate.
• Ternary pair needs three oparands to
operate
• Bitwise operators are those that can
perform bit level manipulations on data
items
Arithmetic Operators

• These operators are used for arithmetic


manipulations
Operator Operation Result
+ (plus) Addition Sum
- (minus) Subtraction Difference
* (astrik) Multiplication Product
/ (Back Slash) Division Quotient
% (Modulo) Division Reminder
Arithmetic Operators Characteristics

• When two like data types participate in an


arithmetic operation, it results in the same data
type, but in case of two dissimilar data types, it
results in the higher prominent data type.
– 7/2 results in 3
– 7.0/2 or 7/2.0 or 7.0/2.0 results in 3.5
• The % (modulo) operator works only on
integers.
• If characters happen to participate in arithmetic
operation, their ascii value is concedered
– ‘A’ + 20 results in 65 + 20 that is 85.
Assignment Operators
• These operators are used to assign (store) a value into a variable.
• Always the RHS value is stored into the LHS variable. Thus RHS
can be a value or a variable or an expression but LHS is always a
variable

Operator Example Remarks


= a=5; Value 5 is assigned into a
b=a+7; Value of (a+7) is assigned into b
c=a; Value of a is assigned to c
+= a += 8; a = a + 8;
-= b -= a; B = b – a;
*= X *= Y; X = X * Y;
/= m /= n; m = m / n;
%= p %= 520; p = p % 520;
Relational Operators
• These operators are used to compare two operands
• If comparison is true then 1 is returned else 0 is returned
to indicate false
• Relation expressions are also called simple conditions

Operator Operation
== Is equal to
< less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
!= Not eqaul to
Logical Operators
• These operators are used to combine two simple
conditions. Logical expressions are also called
compound conditions

Operator Operation
&& And: combines two simple cond’s into a
compound cond., such that the compound
cond., is true only when both the simple
cond’s are true
|| Or : combines two simple cond’s into a
compound cond., such that the compound
cond., is true when at least one of the simple
cond’s is true
! Not: is used to nagate a simple cond.
Ternary Pair Operator

• ?: is a pair of operators used for decision


making operations.
• Syntax:
(cond) ? True_statement : false_ statement ;
• Example1
x>y ? printf(“%d is greater”,x) : printf(“%d
is greater”,y);
• Example2
printf(“%d is greater”(x>y)?x:y);
Unary Minus (-)

• This is used to convert a positive content to


negative and a negative content to positive.
• This operator is used only on the left hand side
of the oparand.
• Example1:
Int x=25;
Int y= -x; y results in -25
• Example2:
Int x=-25;
Int y= -x; y results in 25
Increment / Decrement

• ++ is called increment operator and increases the value


of a variable by one unit
• -- is called the decrement operator and decreases the
value of a variable by one unit.
• These operators if used on the left side of the operand ,
it is called pre increment and pre decrement respectively
• These operators if used on the right side of the operand ,
it is called post increment and post decrement
respectively
• In Pre Operation first incrimination or decrement
happens and then the remaining operations will occur.
• In Post Operation first all other operations will occur and
then at last the incrimination or decrement happens
Inc / Dec Examples

//pre-inc & pre-dec //post-inc & post-dec


Int a,b,c,d; Int a,b,c,d;
a = 12; a = 12;
b = 22; b = 22;
c = ++a; c = a++;
d = --b; d = b--;
This results as below: This results as below:
Value of a = 13 Value of c = 12
Value of c = 13 Value of a = 13
Value of b = 21 Value of d = 22
Value of d = 21 Value of b = 21
Miscellaneous Operators

• sizeof(): this operator is used to retrive the


memory sizse occupied by a variable.
• &(reference): this operator is used to retrieve
the address of the given variable.
• ->(indirection): this operator is used to retrieve
the access member values through pointer
reference.
• *(de-reference): this opertor is used to access
the target value of the pointer variable.
Bitwise Operators

• Operators that manipulate the data bit by bit is


called the bitwise operators.
• The following are the available bitwise operators
– & bitwise and
– | bitwise or
– ^ bitwise xor (exclusive or)
– ~ one’s complimant or bitwise not
– << left shift
– >> right shift
Bitwise And &
• & is defined as
– 0&0 = 1&0 = 0&1 = 0
– 1&1 = 1.

• Example: unsigned int a,b,c; a=4; b=7; c=a&b;

• a in binary is 0000 0000 0000 0100 =4


&&&& &&&& &&&& &&&&
• b in binary is 0000 0000 0000 0111 =7 so
==== ==== ==== ====
• c in binary will be 0000 0000 0000 0100 = 4

• Thus the answer is c results in 4.


Bitwise Or |
• | is defined as
– 1|1 = 1|0 = 0|1 = 1
– 0|0 = 0.

• Example: unsigned int a,b,c; a=4; b=7; c=a|b;

• a in binary is 0000 0000 0000 0100 =4


|||| |||| |||| ||||
• b in binary is 0000 0000 0000 0111 =7 so
==== ==== ==== ====
• c in binary will be 0000 0000 0000 0111 =7

• Thus the answer is c results in 7.


Bitwise XOR ^
• ^ is defined as
– 1^1 = 0^0 = 0
– 1^0 = 0^1 = 1.

• Example: unsigned int a,b,c; a=4; b=7; c=a^b;

• a in binary is 0000 0000 0000 0100 =4


^^^^ ^^^^ ^^^^ ^^^^
• b in binary is 0000 0000 0000 0111 =7 so
==== ==== ==== ====
• c in binary will be 0000 0000 0000 0011 =3

• Thus the answer is c results in 3.


Bitwise Not ~

• ~ is defined as
– ~1 = 0
– ~0 = 1
• Example: unsigned int a,b; a=4; b=~a;
~~~~ ~~~~ ~~~~ ~~~~
• a in binary is 0000 0000 0000 0100 =4
==== ==== ==== ====
• b in binary is 1111 1111 1111 1011 =65531

Thus the answer is b results in 65531.


Left Shift <<

• This will shift the bit set to the left by given


number of places and resultant spaces are
filled with 0 bit.
• Example: unsigned int a,b; a=4; b=a<<3;
• a in binary is 4 = 0000 0000 0000 0100
• b will be 0 0000 0000 0100 000
• Thus b will be equal to 32
p
• Short cut for b=a<<p is b=a*2
Right Shift >>

• This will shift the bit set to the right by


given number of places and resultant
spaces are filled with 0 bit.
• Example: unsigned int a,b; a=65; b=a>>3;
• a in binary is 65 = 0000 0000 0100 0001
• b will be 000 0000 0000 0100 0
• Thus b will be equal to 8
p
• Short cut for b=a>>p is b=a/2

You might also like