Mangalayatan University
Faculty Name:- Prof. LALIT MOHAN GUPTA
Course Name :- Programming with ‘C’
Course Code :- CSM – 6151
Semester :- 1
Class No. :- 5
Block No. :- 2
UNIT No. :- 5,6
10/7/2024 CSM – 6151 Programming with "C" 1
2.
Mangalayatan University
What areoperators
and expressions?
Programs use data stored in variables and perform different
types of operations on that data.
The data on which operations are performed are known as
operands and the types of the operations performed on them
are known as operators.
The combination of operators and expressions are known as
expressions Consider the following c++ statement: z * y
z and y are the operands
* (multiplication is the operator
z * y is an expression
10/7/2024 CSM – 6151 Programming with "C" 2
3.
Mangalayatan University
Operators andType
An operator is a symbol that specifies the mathematical, logical
or relational operation to be performed.
C language supports different types of operators which can be
used with variables and constants to form expressions.
10/7/2024 CSM – 6151 Programming with "C" 3
4.
Mangalayatan University
Type ofOperators(CO2)
Arithmetic operators
Relational operators
Logical operators
Unary operators
Conditional operators
Bitwise operators
Assignment operator
Comma operator
sizeof operator
10/7/2024 CSM – 6151 Programming with "C" 4
5.
Mangalayatan University
Type ofoperators(CO2)
Arithmetic
operators
Relational
operators
Logical
operators
Unary
operators
Conditional
operators
Bitwise
operators
Assignment
operator
Comma
operator
sizeof
operator
10/7/2024 CSM – 6151 Programming with "C" 5
6.
Mangalayatan University
Arithmetic operators
Fora=9, b=3
Operation Operator Example Result
Multiply * a*b 27
Divide / a/b 3
Addition + a+b 12
Subtraction - a-b 6
Modulus % a%b 0
10/7/2024 CSM – 6151 Programming with "C" 6
7.
Mangalayatan University
More Arithmeticoperators
For a=-10, b=3
Operation Operator Example Result
Divide / a/b -3
Modulus % a%b -1
For a=10 ,b=-3
Divide / a/b -3
Modulus % a%b 1
10/7/2024 CSM – 6151 Programming with "C" 7
8.
Mangalayatan University
Relational operators
OperationOperator Example Result
Less than < 3<5 1
Greater than > 7>9 0
LESS THAN OR EQUAL TO <= 100<=100 1
Greater Than or Equal to >= 50>=100 0
Equal to == 20==20 1
Not Equal to != 20!=20 0
10/7/2024 CSM – 6151 Programming with "C" 8
9.
Mangalayatan University
Logical Operators(CO2)
OperationOperator Example Result
Logical AND && (6>5) && (7<9) 1
Logical OR || (6>5) || (7>9) 1
Logical NOT ! !(6>5) 0
Note1: If first operand in False in case of logical AND operation than second
operand will not evaluated
Note2: If first operand in True in case of logical OR operation than second
operand will not evaluated
10/7/2024 CSM – 6151 Programming with "C" 9
10.
Mangalayatan University
Increment(++) /Decrement(--) Operators
The increment(++) and decrement(--) are
unary operators . The syntax is :
Example
s:
The increment / decrement operators do not work on
constants.
Thus
:
Variable++;
or
++variable;
variable --;
or
-- variable;
a++;
++ x;
p=--q;
t--;
6++; //gives error as 6 is a
constant x=--9;
// gives error as 9 is a constant
10/7/2024 CSM – 6151 Programming with "C" 10
11.
Mangalayatan University
Increment(++) /Decrement(--) Operators
The increment operator increases the value of
a variable by 1 The decrement operator
decreases the value of a variable by 1. Thus
and
a++;
is the same
as
a=a+1;
a=7;
a++;
Print(a)
o/p=8
b--;
is the same
as
b=b-1;
10/7/2024 CSM – 6151 Programming with "C" 11
12.
Mangalayatan University
Both theincrement and decrement operators can be
prefixed or postfixed. i.e.
In the above statements both prefix and postfix
forms do not make any difference in the output as
these are stand alone statements.
But when the pre/post increment or decrement
operators are part of an
expression the prefix and postfix notation does matter
++a; (prefix) is
the same as a++;
(postfix)
Increment(++) / Decrement(--) Operators
10/7/2024 CSM – 6151 Programming with "C" 12
13.
Mangalayatan University
Increment(++) /Decrement(-
-) Operators Pre Increment
int a,b;
a=3;
b=++a;
printf(“%d t %d”,a,b);
Consider the following
code snippet:
a=
4
b=
4
1.The value of the variable a is incremented first
2. The new value is assigned to b.
Thus the value of a and b both become 4
1
2
code
10/7/2024 CSM – 6151 Programming with "C" 13
14.
Mangalayatan University
Increment(++) /Decrement(--) Operators Post Increment
In the prefix form the increment or decrement
operation is carried
out before the rest of the expression.
Consider the following
code snippet: a
b
b=a
a=a
+1
a=
4
b=
3
1.The value of the variable a(3) is assigned
to b.
2. The variable a is incremented .
Thus the value of a and b both become 4
2
1
code
int a, b ;
a=3;
b=a++;
printf(“a=%d t b=%d”,a,b);
Output
10/7/2024 CSM – 6151 Programming with "C" 14
15.
Mangalayatan University
Increment/Decrement operators(CO2)
OperationOperator Example Post-fix/Pre-fix
Increment operator ++ Add one to the current value a++/++a
Decrement Operator -- Minus one to the current value a--/--a
Example1: int a=6,b;
b=a++ ; // first assign value than increment the value so b=6,a=7
Example2: int a=6,b;
b=++a; // first increment value than assign the value so b=7,a=7
10/7/2024 CSM – 6151 Programming with "C" 15
16.
Mangalayatan University
Assignment operators(CO2)
OperationOperator Example Assignment
Assignment = Assign values to the left variable a=6
Note: a=6;
a is called lvalue should be variable
6 is rvalue can be variable ,constants and constant expression.
a=b; a=10; a=b+c;
10/7/2024 CSM – 6151 Programming with "C" 16
17.
Mangalayatan University
Points tonote about arithmetic operators
• They can be used with all data types.
• The division operator (/) does integer division only. This
means if we divide say 45 by 6, we will get the answer as 7
and not 7.5 as expected. This is because the / operator
discards the decimal part after division.
Not used 45/7=7. used 75.0/6.0=7.5
• To get the accurate answer in the above case we must write
45.0/6.0.
10/7/2024 CSM – 6151 Programming with "C" 17
18.
Mangalayatan University
01
We canonly
use the
addition and
subtraction
operator
with variables
of char data
type.
02
This is possible
because data (
character or
symbol) is
stored in a
char variable as
an integer
value equal to
the ASCII code
of the
character.
03
In the
statemen
t: char ch
= ‘A’;
the ASCII
equivalent of
‘A
’ i.e. 65 is
stored in ch.
04
So if we write
ch=ch+1;We are
actually adding
1 to 65, which
becomes 66.
Thus ch will now
store the letter
‘B’ whose ASCII
code is 66
Arithmetic operators and char data
10/7/2024 CSM – 6151 Programming with "C" 18
19.
Mangalayatan University
C Shorthands
Theseare operators used to perform an arithmetic
function on an operand and assign the new value
to the operand at the same time.
Operato
r
Description Example Equival
ent to:
+= Addition assignment A+=B A=A+B
-= Subtraction
assignment
A -=B A=A-B
*= Multiplication
assignment
A*=B A=A*B
/= Division assignment A/=B A=A/B
%= Modulus assignment A%=B A=A%B
10/7/2024 CSM – 6151 Programming with "C" 19