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

Operators

Operators are symbols that perform operations on operands. There are three main categories of operators: unary, binary, and ternary. Unary operators act on one operand, binary operators act on two operands, and ternary operators act on three operands. Common operators include arithmetic, relational, logical, assignment, increment/decrement, and bitwise operators. Operators can be used to construct arithmetic expressions and evaluate the relationships between operands. The order of operations is determined by operator precedence and associations.

Uploaded by

Sravan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
31 views

Operators

Operators are symbols that perform operations on operands. There are three main categories of operators: unary, binary, and ternary. Unary operators act on one operand, binary operators act on two operands, and ternary operators act on three operands. Common operators include arithmetic, relational, logical, assignment, increment/decrement, and bitwise operators. Operators can be used to construct arithmetic expressions and evaluate the relationships between operands. The order of operations is determined by operator precedence and associations.

Uploaded by

Sravan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

OPERATORS:

Operator is a symbol that is used to perform a specific operation on operands.


Ex: a+b
here a,b are operands and '+' is the operator
5-4*6
here 5,4,6 are operands and '-' , '*' are operators

Operators are classified into 2 categories:


I) category:
1. Unary Operators
2. Binary Operators
3. Ternary Operators

if an operator acts on only one operand then it is known as unary operator.


Ex: unary +,unary -, ++,--,!,~
-21,+23,
int a=5;
a++;
++a
a--;
--a;
!a
~a

if an operator acts on 2 operands then it is known as binary operator.


Ex: +,-,/,*,%,<,>,<<,>>,&,|,^,etc
5+3
5>6
if an operator acts on 3 operands then it is known as ternary operator.
Ex: ?:
a=10,b=5;
c=a>b?a:b;

II) category:
1. Arithmetic Operators (+,-,*,/,%)
2. Relational Operators(<,>,<=,>=,==,!=)
3. Logical Operators(&&,||,!)
4. Conditional Operator(?:)
5. Assignment Operator(=)
6. Increment and Decrement Operators(++,--)
7. Bitwise Operators(&,|,^,~,<<,>>)
8. Special Operators
--> sizeof operator
--> cast operator
--> comma operator
--> Stringizing Operator (#var)
--> Token pasting operator(##)

UNARY OPERATORS:
If an operator acts on only one operand it is known as Unary Operator.
Ex: ++, --, unary - , unary +, !, ~ etc...
-20
+27
int a=10;
a++;
--a;

BINARY OPERATORS:
If an operator acts on two operands it is known as Binary Operator.
Ex: +,-,*,/,%,&,|,>,<, etc...
a+b*c-d

TERNARY OPERATOR:
If an operator acts on three operands then it is known as ternary operator.
Ex: ?:
a=10;
b=15;
c = a>b?a:b;
5>3?4<8?4:8:2>10?2:1!=0?1:0;

ARITHMETIC OPERATORS:
unary arithmetic operators: ++, --
binary arithmetic operators: +,-,*,/,%
+,-,*,/,% are known as arithmetic operators
these are binary operators.
when we write + and - before the operand it is unary operators and when we write +
and - after the operand it is binary operators.
a = 5+3;--> a=8
b=7-; --> error
c = -8; -->c=-8 sign of the number
d = +12; -->sign
* ,/ ,% -->1st priority
+,- -->2nd priority

Ex1: 5+3-2 -->8-2 --> 6


5+3*8 -->5+24 --> 29
5+3*5-2 -->5+15-2 --> 20-2 --> 18
5*15/3 --> 75/3 = 25
when we are using multiple operators in one expression and all will have equal
priority then the evaluation is from left to right if those are binary operators.
If operators are unary then evaluation is from right to left.

/ and %:
/ - division operator will give quotient as the output
% - modulo division operator will give remainder as the output
5/2 -->2(quotient)
5%2 -->1(reminder)
-5/2 -->-2(quotient)
5/-2 -->-2
-5/-2 = 2
if numerator is less than denominator in integer division the output is always 0
in prog lang integer/integer gives integer only
2/10 -->0
2.0/10 --> 0.2
modulus operator is not applicable for floating data type variables
5.0%2 --> error
5.0/2 -->2.5
fmod(double, double) --> a = fmod(5.0,2.0) -->

102/10 -->10
128/10 -->12
265/10 -->26
102%10 -->2
128%10 -->8
265%10 --> 5
ARITHMETIC EXPRESSION:
when two or more operands are combined by using arithmetic operators then that
expression is known as "Arithmetic expression"
Ex:a+b
5-6*3 = 5-18 = -13
10/2*3 =5*3 = 15
--------------------------------------------------------------------
RELATIONAL OPERATORS:
<, >, <=, >=, ==, !=
these operators are used to compare two or more operands or arithmetic expressions
2>3
2+3 >= 2-3
--> these operators returns 1 when condition is true.
and returns 0 when condition is false.
-->all non zero values considers as true i.e, returns 1
Ex:
5>3<2==1 --> 1<2==1 --> 1==1 -> true ->1
--------------------------------------
PRIORITIES:
()
!,unary +,unary -,++,-- (prefix)
* / %
+,-
< > <= >=
== !=
&&
||
?:
=
++,-- (postfix)
---------------------------------------------------------------
LOGICAL OPERATORS:
&& - Logical AND
|| - Logical OR
! - Logical NOT

Logical operators are used to combine 2 or more relational expressions


Ex: 2>3 && 2<3
2>3 || 2<3

R1 R2 R1&&R2 R1||R2
T T T T
T F F T
F T F T
F F F F

Logical AND is true when all expressions are true


Logical OR is false when all expressions are false
!true = false
!false = true
-----------------------------------------------------------------
INCREMENT AND DECREMENT OPERATORS:

"++" is known as increment operator


"--" is known as decrement operator
++ adds one to the previous value of variable
-- subtracts one from the previous value of variable
when we are working with increment and decrement operators then modification
between old value and new value will be +1/-1.
++ adds one to the previous value of variable.
-- subtracts one from the previous value of variable
-->when we use prefix increment or decrement operator, first value is incremented
or decremented and then the updated value will be printed.
-->when we use postfix increment or decrement operator, first value is printed and
then internally it will be incremented or decremented.

Depends on the position of this operators, these are classified into 2 types:
Prefix
Postfix

int a=5;
++ :
1. Prefix increment operator
Ex: ++a;
2. Postfix/suffix increment operator
Ex: a++;

--:
1. Prefix Decrement operator
Ex: --a;
2. Postfix or suffix decrement operator
Ex: a--;

a=5;
a++; -->6
++a; -->6
int b=5;
b--;
--b;

(prefix ++,--
*/%
+ -
=
postfix ++,--)

Ex:
int a=1;
a=++a + ++a + ++a;==>a=a+a+a; ==>a=4+4+4=12
printf("a=%d",a);

a=1;
a=++a + a++ + ++a;
a=a+a+a=3+3+3=9
a=9+1 = 10

a=1;
a=a++ + ++a + a++;
a=a+a+a=2+2+2=6
a=6+1+1 = 8

BITWISE OPERATORS:
bitwise operators performs operations on bits internally.
there are six bitwise operators:
& - Bitwise AND operator
| - Bitwise OR operator
^ - Bitwise XOR operator (Exclusive OR)
~ - Bitwise one's Complement
<< - Bitwise Left shift operator
>> - Bitwise Right shift operator.

b1 b2 b1&b2 b1|b2 b1^b2


1 1 1 1 0
1 0 0 1 1
0 1 0 1 1
0 0 0 0 0

You might also like