Lecture Operators Part1
Lecture Operators Part1
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
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–
6
Unary arithmetic operators
• An unary operator needs single operands to perform the operation.
• 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);
}
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.
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)
}
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
• 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 + -
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.
=1
17
Exercise
int x= 30;
printf ( “\n\t ++x=%d and x++ = %d”,++ x, x++);
18
Precedence in Expression
1+2*3–4/5=1+(2*3)–(4/5)
B.O.D.M.A.S
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
• 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
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; }
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:
27
Example:
• OUTPUT:
Logical operator or Boolean Operator
29
Logic AND
30
Example 1
( 9< 4 ) && ( 6 > 7)
0 && 0
False ( 0 )
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
• 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.
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.
• 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 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.
• 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;
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.
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.
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