Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
21 views

Lecture Operators Part1

Uploaded by

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

Lecture Operators Part1

Uploaded by

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

Programming Fundamental CO101

Operators in C
Outline
• Assignment Operators

• Arithmetic Operators

• Relational Operators

• Logical Operators

• Bitwise Operators

• Assignment Operators

• Misc Operators

2
Operators
• An operator are used to perform operation on the variables that yields a value.
• The variables, constants are used an operand with operator to form an
expression.
• An operator is a symbol that tells the compiler that what operation needs to
perform on the operands.
• Example : “ a + b “ : + is an add operator that adds two operands a and b.
• Some operators require two operands, while others act upon only one operand.

3
Expression
• An expression is formed using the operands such as constants, variables,
and numbers with operators and parenthesis.

• Example :
(x*y)+z

In the example x , y and z are operands. * and + are operators.

4
C language provides the following types of operators:

• Arithmetic Operators

• Relational Operators

• Logical Operators

• Bitwise Operators

• Assignment Operators

• Misc Operators

5
Arithmetic operators
Arithmetic operators are used to perform arithmetical operation on the
operand.
There are two types of arithmetic operation–

1. Unary arithmetic operators

2. Binary arithmetic operators

6
Unary arithmetic operators
• An unary operator needs single operands to perform the operation.

• Unary arithmetic operators are:


• Unary minus ( - )
• Increment ( + +)
• decrement ( - - )

• Unary operators are RIGHT associative means they are evaluated from right to
left when operators of same precedence are encountered in an expression.

7
Unary minus ( - )
• Unary operator are written before any numeric value, variable or expression.
For example:
-y here ‘-’ means change the sign of operand y.

• When unary operator are used on any operands, means it negates the value of
operand.
• Following are few examples of unary operators:
-57
- (a*b)= it negates the value calculated by the operation a*b
-x = it negates the value of x. If x value is negative then it will become
positive and if x value is positive then it becomes negative.
8
Increment operator ( ++ )
• The increment ( ++ ) operator is a unary operator because it apply on single operand.
• It adds 1 to its operand.
• increment operator represented as:
++operand/operand++
• ++operand is called a prefix increment. It increments the value of operand before it used.
• operand++ is called as postfix increment. It used operand first then increments the value of
operand.

Example: x =4
Prefix increment: a= ++ x :x value first updated. a = 5, x=5
Postfix increment: a= x++ :x value used directly then updated. a=4, x=5

9
Example 1
int m=15, prem, postm;
prem = ++m;
postm= m++
Printf(“\n m = %d, m);
Printf(“\n prem=%d, postm=%d,prem,postn);

Output:
m = 17
prem = 16, postm = 16

10
Exercise
Write the output of following:
void main()
{
int a =3, b=6, c;
c = (++a+b++);
++c;
printf(“a =%d, b= %d, c=%d”,a,b,c);
}

Output? a =4, b= 7, c=11

11
Decrement operator
• Decrement operator works same as increment operator.
• Decrement ( - - ) operator subtracts 1 from its operand.
• Decrement operator syntax:

--operands/operands--
• Postfix decrement ( operand- -) : In this case value of operand is fetched before
subtracting 1 from it.

• Prefix decrement ( - - operands) : In this case value of operand is fetched after


subtracting 1 from it.

12
Example 1
• int m=15, predm, postdm;
• predm = --m;
postdm= m--
• Printf(“\n m = %d, m);
• Printf(“\n predm=%d, postdm=%d,predm,postdm);

Output:
m = 13
predm = 14, postm = 14

13
Example
Write the output of following:
void main()
{
int a =6, b=3, c;
c = (--a-b--);
c--;
printf(“a =%d, b= %d, c=%d”,a,b,c)
}

Output? a =5, b= 2, c=1

14
Binary arithmetic operators
• A binary operator performs arithmetic operation on the two operands.
• There are five binary operator.
+ addition
- subtraction
* multiplication
/ division
% gives the remainder in integer division

• % (modulus operator) cannot be applied with floating point operands.

• There is no exponent operator in C. However there is a library function pow( ) to carry out
exponentiation operation.
15
Precedence of Arithmetic operators

Highest :
++ --
- (unary minus)
* / %
Lowest + -

here ++ , -- are on same level and have higher precedence


%, /, *are one same level but precedence lower than -.
+ and – are on same level but precedence lower than %, /, *

16
Associativity
• Associativity is defined as the order in which consecutive operation with in the same precedence
group will be carried out.
• Operators on same level of precedence are evaluated by the complier from left to right (Left
associative).
• For a complex expression it becomes difficult to make out as to in what order the evaluation of sub
expression would take place.
• In such cases we checkout the precedence and associativity.

E.g. in the expression


= 2+3-4
= 5-4

=1

17
Exercise
int x= 30;
printf ( “\n\t ++x=%d and x++ = %d”,++ x, x++);

++x = 32 and x++ = 30

18
Precedence in Expression

1+2*3–4/5=1+(2*3)–(4/5)

B.O.D.M.A.S

B stands for brackets


O for order (exponents)
D for division
M for multiplication
A for addition
S for subtraction

19
Example
int x;
output:
x= 7 + 3 * 5;
x = 22

int x; output:
x= ( 7 + 3) * 5; x = 50

int x; output:
x= 7 / 3 * 5; x = 10

20
Modulus operator ( % )
• This operator has same priority as that of multiplication and division

• a % b = output remainder after performing a / b

• Example:
7 % 10 = 7
7%1 = 0
7%2 = 1
7%7 =0

21
Exercise
int y; Output:
y = 10 % 4 * 3; 6

int y; Output::
y = 3 * 10 % 4; 2

 a * + b : Invalid as two operators can not be used in


continuation.

 a( b * c ) : Invalid as there is no operator between a and b.

22
Important
• Modulus operator ( % ) : It produces remainder of an integer
division. This operator can not be used with floating point
numbers.
int main( ){
float f_1=3.2, f_2=1.1, f_3 ;
f_3 = f_1 % f_2;
printf(“ %f”, f_3);
return 0; }

Output: error at line 3


illegal use of floating point
23
Example:
• OUTPUT:
Relational & Logical operators
• A relational operators are used to compare two operands.
• The result of such operation is always logical either TRUE ( 1 ) or
FALSE ( 0 ).
Following are the relational operators in C
< less than a<b
 > greater than a> b
 <= less than or equal to a <= b
 >= greater than or equal to a>= b
 == is equal to a==b
 != is not equal to a != b

25
Following table shows all the relational operators supported by C language.
Assume variable A holds 10 and variable B holds 20, then:
Operator Description Example
== Checks if the values of two operands are equal or A==B not
not, if yes then condition becomes true. true
!= Checks if the values of two operands are equal or A!=B true
not, if values are not equal then condition becomes true
> Checks if the value of left operand is greater than the value A>B is false
of right operand, if yes then condition becomes true.
< Checks if the value of left operand is less than the value of A<B is true
right operand, if yes then condition becomes true.
<= Checks if the value of left operand is less than or equal to the A <= B is
value of right operand, if yes then condition becomes true. true.
>= Checks if the value of left operand is greater than or equal to A >= B false
the value of right operand, if yes then condition becomes
true.

26
Exercise
• Suppose that a,b, and c are integers variables.
What will be the value of following expressions:

int a =10, b=5 , c =15;


a<b; False
b<c; True
True
(a+b)>=c; False
(b+10)==(a+c); false
a!=10;

27
Example:
• OUTPUT:
Logical operator or Boolean Operator

 Logical operators are used to perform logical operations on the


given expression.
 For combining expressions we use logical operators.
 These operators return 0 for false and 1 for true.
 The operands can be constants, variables or expressions.
 There are 3 logical operators in C language.

&& Logical AND a&& b


|| Logical OR a || b
! Logical NOT !a

29
Logic AND

The output of AND operation is TRUE if BOTH the operands are


true.
Boolean Table

Condition1 Condition2 Result


False False False
False True False
True False False
True True True

30
Example 1
( 9< 4 ) && ( 6 > 7)

0 && 0

False ( 0 )

(9 > 4) && (2 < 3)

1 && 1

True ( 1 )
31
Logical OR
• The result of a logical or operation will be true if either operand is true or if both operands are
true.

Boolean Table

Condition1 Condition2 Result


False False False
False True True
True False True
True True True

(6< 4) | | (3 > 5) is false


(6 > 4) | | (3 > 5) is true
(6 > 4) | | (3 < 5) is true
32
Logical NOT
• The Logical NOT ( ! ) is a unary operator. It negates the value of the logical expression or
operand.

Boolean Table Condition Result


True False
False true

• If value of a = 0 !a=?
! a= 1
• ! ( 3< 5 ) || ( 9>9 ) = ?
! ( 1) || ( 0) = ! 1 = 0 -> false
• !(34 >= 765) = ??
33
-> 1 -> True.
Example:
• OUTPUT:
Conditional Operator
• Conditional operators required three operands or expression to execute the operation.
• That’s why it is called as a ternary operator ( ? and: ) .
• This written as:
TestExpression ? expression1 : expression2
• Firstly the TestExpression is evaluated.
• If TestExpression is true(nonzero), then expression1 is evaluated and it becomes the value of the overall
conditional expression.
• If TestExpression is false(zero), then expression2 is evaluated and it becomes the value of overall
conditional expression.

35
Example:
a = 5, b = 8,
max = a > b ? a : b;
In the expression a > b is evaluated, since it is false so the value of b becomes the
value of conditional expression and it is assigned to variable max.

EX2: a < b ? printf("a is smaller") : printf("b is smaller");


Since the expression a < b is true, so the first printf function is executed.

Output: a is smaller

36
Comma Operator
• Comma operator (,) is a binary operator.

• When there are multiple expressions in a single line then that expressions will be
separated by using comma operator.

• These expressions are evaluated one by one.

• The comma separated expressions are evaluated from left to right.

• The value of rightmost or last expression will be the value of the compound expression.
37
• Example:
a = 8, b = 7, c = 9, a+b+c

• Here we have combined 4 expressions. Initially 8 is assigned to the variable a, then 7 is


assigned to the variable b, 9 is assigned to variable c and after this a+b+c is evaluated
which becomes the value of whole expression.
• So the value of the above expression is 24.
sum = ( a = 8, b = 7, c = 9, a+b+c );

• Here the value of the whole expression on right side will be assigned to variable sum
i.e. sum will be assigned value 24.
• Since precedence of comma operator is lower than that of assignment operator hence
the parentheses are necessary here. The comma operator helps make the code more
compact

38
sizeof Operator
• sizeof is an unary operator.

• This operator gives the size of its operand in terms of bytes.

• The operator can be a variable, constant or any datatype( int, float, char etc ).

• For example sizeof(int) gives the bytes occupied by the int datatype i.e. 2

sizeof(float) = 4 byte

sizeof(char) = 1 byte

39
Operations using Assignment operators
• Logical operators allowed following operations in C:
• Assignment(=)
• Addition Assignment(+=)
• Subtraction Assignment(-=)
• Multiplication Assignment(*=)
• Division Assignment(/=)
• Remainder Assignment(%=)
Assignment operator
• It is used to assign a value to the variable:
variable_name = expression;

• lvalue : In compiler lvalue error messages means that an object


on left hand side of assignment operator is missing.

• rvalue : In compiler rvalue error messages means that expression


on right hand side of assignment operator is erroneous.

41
Two cases of assignment
• Multiple assignment:
int j=k=m=0;

• Compound assignment:
x= x+5; this expression can be written as
x + = 5;
similarly
y= y-100; is equivalent to y - = 100;

42
Example:
• OUTPUT:
Bitwise Operators
• C has the ability to support the manipulation of data at the bit level.

• Bitwise operators are used for operations on individual bits.

• Bitwise operators operate on integers only. The bitwise operators are:

Source: Srivastava, S. K., and Deepali Srivastava. C in Depth. BPB Publications, 2018. 44
Example:
• OUTPUT:
Operator precedence: It dictates the order of
evaluation of operators in an expression.

Associativity: It defines the order in which


operators of the same precedence are evaluated
in an expression. Associativity can be either
from left to right or right to left.

Source: https://overiq.com/

47
Exercise
#include<stdio.h>
main() {
char x;
int y;
x=100;
y=125;
printf("%c\n",x); Output:
d
printf("%c\n",y);
}
printf("%d\n",x); 100
return 0;
}

48
#include<stdio.h>
#include<stdio.h>
main() {
int x=100;
printf("%d\n",10 + x++);
printf("%d\n",10 + ++x);
return 0; Output:
} 110
112

49
50
• https://www.tutorialspoint.com/cprogramming/c_relational_operators.htm\
• https://www.programiz.com/c-programming/c-operators
• Srivastava, S. K., and Deepali Srivastava. C in Depth. BPB Publications, 2018.

51
The End

You might also like