2. Operator
• Is a symbol that directs the computer to perform certain operations.
• Operations are performed on operands.
• Operation is an action performed on one or more operands.
• Operations can be mathematical or logical.
• Operands can be a variable, constant, or an expression.
• Are used to manipulate data and variables.
• Usually form a part of mathematical or logical expressions.
• Can be unary, binary or ternary.
By:SatveerKaur,AssistantProfessor,Khalsa
College,Patiala.
2
4. Arithmetic Operators
+
•Addition or unary plus
• a+b
-
•Subtraction or unary minus
• a-b
*
•multiplicatiion
• a*b
/
•Division
• a/b
%
•Modulo division
• a%b
By:SatveerKaur,AssistantProfessor,Khalsa
College,Patiala.
4
5. Types of Arithmetic operators
Integer arithmetic
Real arithmetic
Mixed-mode
arithmetic
By:SatveerKaur,AssistantProfessor,Khalsa
College,Patiala.
5
6. Relational operators
<
• is less than
• a<b
<=
• is less than or equal to
• a<=b
>
• is greater than
• a>b
>=
• is greater than or equal to
• a>=b
==
• is equal to
• a==b
!=
• is not equal to
• a!=b
By:SatveerKaur,AssistantProfessor,Khalsa
College,Patiala.
6
7. Logical operators
&& - Logical AND
|| - Logical OR
! - Logical NOT
By:SatveerKaur,AssistantProfessor,Khalsa
College,Patiala.
7
11. Rules for ++ and - - operator
Are unary operators and require variables as their operands.
When postfix ++ (or --) used with a variable, expression is evaluated with
the original value of the variable and then incremented or decremented.
When prefix ++v(or --) used, the variable is incremented first and
expression is evaluated using the new value of the variable.
Precedence and associativity is same as unary + or unary -.
By:SatveerKaur,AssistantProfessor,Khalsa
College,Patiala.
11
12. Conditional operator
It is a ternary operator.
exp1?exp2:exp3
exp1, exp2, exp3 are expressions.
x=(a>b)?a:b;
By:SatveerKaur,AssistantProfessor,Khalsa
College,Patiala.
12
13. Bitwise operator
&
• Bitwise AND
|
• Bitwise OR
^
• Bitwise exclusive OR
<<
• Shift left
>>
• Shift right
By:SatveerKaur,AssistantProfessor,Khalsa
College,Patiala.
13
14. Special operators
The comma
operator
The sizeof
operator
• Can be used to link the
related expressions.
• e.g. x=10, y=15
• It is a compile time operator.
• Returns the number of bytes
the operand occupies.
• e.g. m=sizeof(sum);
n=sizeof(long int);
By:SatveerKaur,AssistantProfessor,Khalsa
College,Patiala.
14
15. Expressions
Is a formula
Operands are linked together by the use of operators to
calculate a value.
Operand can be a function, a variable, a constant or an
array.
e.g. a-b; c+d; etc.
By:SatveerKaur,AssistantProfessor,Khalsa
College,Patiala.
15
17. Precedence & associativity of
operators
By:SatveerKaur,AssistantProfessor,Khalsa
College,Patiala.
17
Precedence rules decide the order in which
different operations are applied.
Associativity rules decide in which multiple
occurrences of the same level operator are
applied.
18. Precedence & associativity
By:SatveerKaur,AssistantProfessor,Khalsa
College,Patiala.
18
Operator Description Associativity Rank
( ) Function call Left to Right 1
[ ] Array element reference
+ Unary plus Right to left 2
- Unary minus
++ Increment
-- Decrement
! Logical negation
~ One’s complement
* Pointer reference(indirection)
& Address
sizeof Size of an object
(type) Type cast(conversion)
19. Precedence & associativity
Operator Description Associativity Rank
* Multiplication Left to Right 3
/ Division
% Modulus
+ Addition Left to right 4
- Subtraction
<< Left shift Left to right 5
>> Right shift
< Less than Left to right 6
<= Less than or equal to
> Greater than
>= Greater than or equal to
== Equality Left to right 7
|= Inequality
By:SatveerKaur,AssistantProfessor,Khalsa
College,Patiala.
19
20. Precedence & associativity
Operator Description Associativity Rank
& Bitwise AND Left to right 8
^ Bitwise XOR Left to right 9
| Bitwise OR Left to right 10
&& Logical AND Left to right 11
|| Logical OR Left to right 12
? : Conditional expression Right to left 13
= Assignment operators Right to left 14
*=, /=, %=
+=, -=, &=
^=, |=
<<=, >>=
, Comma operator Left to right 15
By:SatveerKaur,AssistantProfessor,Khalsa
College,Patiala.
20