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

Chapter 3 Operator

The document provides an overview of operators in programming, categorizing them into unary, binary, and ternary operators based on the number of operands they require. It further classifies operators into various types, including arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, and special operators, detailing their functions and examples. Each type of operator is explained with examples to illustrate their usage in programming.

Uploaded by

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

Chapter 3 Operator

The document provides an overview of operators in programming, categorizing them into unary, binary, and ternary operators based on the number of operands they require. It further classifies operators into various types, including arithmetic, relational, logical, assignment, increment/decrement, conditional, bitwise, and special operators, detailing their functions and examples. Each type of operator is explained with examples to illustrate their usage in programming.

Uploaded by

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

Prepared By: ER.

Kamal Gyawali

Chapter -3

Operator:[2015 fall, 2016 fall,2017 fall, 2016 spring,2019 spring]


 Operator is a symbol that operates on a certain data type or data item.
 Operators are used in programs to perform certain mathematical or logical
manipulations.
 For example, in a simple expression 8+9, the symbol + is called operator
which operates on two data items 8 and 9.
 On the basis of operand required there are three types of operator. They
are as follows:
a) Unary operators
b) Binary operators
c) Ternary operators

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

According to the utility and action, operators are


classified into various categories:
i. Arithmetic operators
ii. Relational operators
iii. Logical operators
iv. Assignment operators
v. Increment and decrement operators
vi. Conditional operators
vii. Bitwise operators
viii. Special operators

1) Arithmetic operators:
 Arithmetic operators perform arithmetic
operations.
 There are five arithmetic operators in c.

Operator Meaning Example Output


int a=20,
b=6
+ Addition a+b 26
- Subtraction a-b 14
* Multiplication a*b 120
/ Division a/b 3
% Modulo a%b 2
division

2) Relational operators:
 Relational operators are used to compare two
operands.
 There are 6 relational operators in c.
Prepared By: ER. Kamal Gyawali

Operator Meaning Example Output


int a=20,
b=6
< Less than a<b 0
> Greater than a>b 1
<= Less than or a<=b 0
equal to
>= Greater than a>=b 1
or equal to
== Equal to a==b 0
!= Not equal to a !=b 1

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

5) Increment and decrement operators:


 The increment operator is used to increase the
value of an operand by 1.
 The decrement operator is used to decrease
the value of an operand by 1.
 They take only one operand, so called unary
operator.
 The syntax for the operator is
++ Variable [variable=variable+1]
Variable ++ [variable=variable+1]
--variable [variable=variable-1]
Variable -- [variable=variable-1]
 ++a and --a are known as prefix notation.
 a++and a--are known as postfix notation.

/*example program to illustrate prefix and


postfix notation of increment and decrement
operator*/
#include <stdio.h>

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

Bitwise one‟s complement:


N1 10110001
~N1 01001110

8) Special operators:
 C supports some special operators such as
comma operator (,), size of operator, pointer
operators (& and *) and member selection
operators(. And ->).

You might also like