0% found this document useful (0 votes)
9 views18 pages

Operators

Uploaded by

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

Operators

Uploaded by

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

OPERATORS

Introduction
• Operators are the symbols which operates on value or a variable. It tells the compiler to
perform certain mathematical or logical manipulations.
• Can be of following categories:
• Unary – requires a single operand
• Binary – requires two operands
• Ternary – requires three operands
• Following are the types of operators:
• Arithmetic
• Relational
• Logical
• Bitwise
• Assignment
• Conditional (ternary)
• Increment/Decrement
• Special
Types of operators
Types of operators Description
Arithmetic operators (binary) These are used to perform mathematical calculations like addition,
subtraction, multiplication, division and modulus.
Assignment operators (binary) These are used to assign the values for the variables in programs.
Relational operators (binary) These operators are used to compare the value of two variables.
Logical operators (binary) These operators are used to perform logical operations on the
given two variables.
Bitwise operators (binary) These operators are used to perform bit operations on given two
variables.
Conditional operators (ternary) Conditional operators return one value if condition is true and
returns another value is condition is false.
Increment/Decrement operators These operators are used to either increase or decrease the value
(unary) of the variable by one.
Special operators sizeof( )
Arithmetic Operators
Operator Description Example (A is 10, B is 20)
+ Adds two operands A + B will give 30
- Subtracts second operand from the first A – B will give -10
* Multiplies both operands A * B will give 200
/ Divides numerator by de-numerator B/A will give 2 and A/B will give 0.5(if float) and
0(if int)
% Modulus Operator and remainder of after B % A will give 0 and A % B will give 10
an integer division

• General form is operand1 operator operand2


Assignment Operators
Operator Description Example
= Simple assignment operator, Assigns values from
C = A + B will assign value of A + B into C
right side operands to left side operand
+= Compound assignment operator, Add and C += A is equivalent to C = C + A
assignment operator. It adds right operand to the
left operand and assign the result to left operand
-= Compound assignment operator, Subtract and C -= A is equivalent to C = C - A
assignment operator. It subtract right operand
from the left operand and assign the result to left
operand, others would be “ *=, /=, %= “
<<= Left shift and assignment operator C <<= 2 is same as C = C << 2
>>= Right shift and assignment operator C >>= 2 is same as C = C >> 2
&= Bitwise and assignment operator, others would be C &= 2 is same as C = C & 2
“^=, |=“

• General form is v op= exp where v is a variable and op is a binary arithmetic operator.
This statement is equivalent to v = v op(exp)
Relational Operators
Operator Description Example (A is 10, B is 20)
== Checks if the values of two operands are equal or not, if yes then
A == B will give 0
condition becomes true.
!= Checks if the values of two operands are equal or not, if values A != B will give 1
are not equal then condition becomes true.
> Checks if the value of left operand is greater than the value of A > B will give 0
right operand, if yes then condition becomes true.
< Checks if the value of left operand is less than the value of right A < B will give 1
operand, if yes then condition becomes true.
>= Checks if the value of left operand is greater than or equal to the A >= B will give 0
value of right operand, if yes then condition becomes true.
<=
Checks if the value of left operand is less than or equal to the
A <= B will give 1
value of right operand, if yes then condition becomes true.

• General form is operand1 relational-operator operand2 takes a value of 1(int) if the


relationship is true and 0(int) if relationship is false.
Logical Operators
Operator Description Example (A is 10, B is 20)
&& Called Logical AND operator. If both the operands are non-zero, (A>=10) && (B>15) will give 1
then condition becomes true. (A>10) && (B>15) will give 0
|| Called Logical OR Operator. If any of the two operands is non- (A>10) || (B>15) will give 1
zero, then condition becomes true.
! Called Logical NOT Operator. Use to reverses the logical state of (A>10) will give 0
its operand. If a condition is true then Logical NOT operator will !(A>10) will give 1
make false.

• General form is operand1 logical-operator operand2 takes a value of 1(int) if the


relationship is true and 0(int) if relationship is false.
• expr1 && expr2 has a value 1 if expr1 and expr2 both are nonzero.
• expr1 || expr2 has a value 1 if expr1 and expr2, any one of them is nonzero.
• !expr1 has a value 1 if expr1 is zero else 0.
Bitwise Operators

Operator Description Example (A is 60, B is 13)


& Binary AND Operator copies a bit to the result if it exists in both
(A & B) = 12 (0000 1100)
operands.
| Binary OR Operator copies a bit if it exists in either operand. (A | B) = 61 (0011 1101)

~ Binary NOT Operator is unary and has the effect of 'flipping' bits. (~A) = -61 (1100 0011 in 2’s complement
form)
^ Binary XOR Operator copies the bit if it is set in one operand but (A ^ B) = 49 (0011 0001)
not both.
<< Binary Left Shift Operator. The left operand’s value is moved left A << 2 will give 240 which is 1111 0000
by the number of bits specified by the right operand.
>> Binary Right Shift Operator. The left operand’s value is moved A >> 2 will give 15 which is 0000 1111
right by the number of bits specified by the right operand.
Bit Wise Complement Operator
1. For any integer n, the bitwise complement of n will be -(n+1).

2. Bitwise complement of N = ~N (represented in 2’s complement form).

3. 2’complement of ~N= -(~(~N)+1) = -(N+1).


Increment/Decrement Operators

• Increment operators are used to increase the value of the variable by one and decrement operators
are used to decrease the value of the variable by one in C programs.
• Syntax:
• Increment operator: ++var_name; (or) var_name++;
Decrement operator: – -var_name; (or) var_name – -;
Conditional Operator
•A ternary operator pair “?:” is available to construct conditional expression of
the form
• Exp1 ? Exp2 : Exp3 where Exp1, Exp2 and Exp3 are expressions.
• The operator ?: works as follows: Exp1 is evaluated first. If it
is nonzero (true), then the expression Exp2 is evaluated and
becomes the value of the expression.
• If Exp1 is false, Exp3 is evaluated and its value becomes the
value of the expression.
• Syntax
• (Condition? true_value: false_value);
• E.g. a = 10;
b = 15;
x = (a > b) ? a : b
Special Operators
• To get the exact size of a type or a variable on a particular platform, you can use
the sizeof operator. The expressions sizeof(type) yields the storage size of the
object or type in bytes.
• int a; sizeof(a)  2 bytes (in 32 bit machine) and 4 bytes (in 64 bit machine)
• float b; sizeof(b)  4 bytes
• double c; sizeof(c)  8 bytes
• char d; sizeof(d)  1 byte
Precedence and Associativity
• Precedence and Associativity are two characteristics of operators that determine
the evaluation order of subexpressions in absence of brackets.
• Please check document of precedence and associativity.

• Some points:
• All operators with same precedence have same associativity  This is necessary,
otherwise there won’t be any way for compiler to decide evaluation order of
expressions which have two operators of same precedence and different
associativity. For example + and – have same associativity.
• Associativity is only used when there are two or more operators of same
precedence.
• PUMASREBLTAC (PRECEDENCE)

• TAU (RIGHT TO LEFT)


Examples
•3+4*4>5*(4+3)-1
•x=5,y=5,z
z=y + x * y-- + ++x;
printf(“%d%d%d”,x,y,z);
Example
a=0, b=1,c=-1
d= --a*(5+b)/2-c++*b;
printf(“%d”,d);
THANK YOU
ANY QUESTIONS???

You might also like