Chapter 3 Operator
Chapter 3 Operator
Kamal Gyawali
Chapter -3
1) Unary operators:
The operators which require only one operand are known as
unary operators.
For example, ++ (increment operator), -- (decrement
operator), + (unary plus), - (unary minus) are unary operators.
2) Binary operators:
The operators which require two operands are known as
binary operators.
For example, + (plus), - (minus), * (multiply), / (division), < (less
than), > (greater than), etc. are binary operators.
3) Ternary operator:
The operators that require three operands are known as
ternary operators.
For example, the operator pair “?:” (conditional operator) is a
ternary operator.
Prepared By: ER. Kamal Gyawali
1) Arithmetic operators:
Arithmetic operators perform arithmetic
operations.
There are five arithmetic operators in c.
2) Relational operators:
Relational operators are used to compare two
operands.
There are 6 relational operators in c.
Prepared By: ER. Kamal Gyawali
3) Logical operators:
Logical operators are used to compare or
evaluate logical and relational expressions.
There are 3 logical operators in c:
&& Logical AND
|| Logical OR
! Logical NOT
a b a&&b a||b !a !b
0 0 0 0 1 1
0 1 0 1 1 0
1 0 0 1 0 1
1 1 1 1 0 0
4) Assignment operators:
Assignment operators are used to assign the
result of an expression to a variable.
The usual assignment operator is „=‟.
They are other various shorthand assignment
operators also. They are +=, -=, *=, /= and %=.
For example:
a +=b a=a+b
Prepared By: ER. Kamal Gyawali
a -=b a=a-b
a *=b a=a*b
a /=b a=a/b
a%=b a=a%b
int main()
{
int a=10;
printf("a=%d\n",a);
printf("a=%d\n",++a);
Prepared By: ER. Kamal Gyawali
printf("a=%d\n",a++);
printf("a=%d\n",a);
return 0;
}
Output:
6) conditional operator:
The operator pair “?:” is known as conditional
operator.
It takes three operands so it is also called
ternary operator.
Prepared By: ER. Kamal Gyawali
Output:
7) Bitwise operator:
Bitwise operators are used for manipulating
data at bit level.
There are three types of bitwise operators:
i. Bitwise logical operators
ii. Bitwise shift operators
iii. One‟s complement operator
Bitwise AND(&):
N1 01100011
N2 11001010
N3= N1&N2
N3 01000010
Prepared By: ER. Kamal Gyawali
Bitwise OR(|):
N1 01100011
N2 11001010
N3= N1|N2
N3 11101011
8) Special operators:
C supports some special operators such as
comma operator (,), size of operator, pointer
operators (& and *) and member selection
operators(. And ->).