Operators
Operators
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
/ 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
R1 R2 R1&&R2 R1||R2
T T T T
T F F T
F T F T
F F F F
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.