Download presentation
Presentation is loading. Please wait.
1
Chapter-3 Operators
2
C supports a rich set of operator
An operator is a symbol that tell the computer to perform certain mathematical or logical manipulation.
3
C language uses many types of operators as
listed below:- 1) Arithmetic Operators 2) Relational Operators 3) Logical Operators 4) Increment and Decrement Operators 5) Assignment Operators 6) Conditional Operators 7) Bitwise Operators 8) Special Operators
4
Arithmetic Operators C provides all the basic arithmetic operators.
The operator +,-,* and / all work same way as in other languages. These can operate on any built-in data type allowed in C.
5
Arithmetic Operators Operator Meaning + Addition or unary plus
Subtraction or unary minus * Multiplication / Division % Modulo division (reminder)
6
Integer Arithmetic When both the operands in a single arithmetic expression such as a+b are integers, the expression is called the integer expression, and the operation is called as integer arithmetic. Integer arithmetic always results in an integer value. The largest integer value depends on the machine.
7
Integer Arithmetic Here, a and b are integers, if a= 14 and b=4 then,
a – b = 10 a + b = 18 a * b = 56 a / b = 3 (decimal part truncated) a % b = 2 (reminder of division) 7
8
During modulo division, the sign of
the result is always the sign of the first operand(the dividend). i.e, -14 % 3 = -2 -14 % -3 = -2 but, 14 % -3 = 2
9
In integer division, if both operands
are of same sign, the result is truncated towards 0. But if one of them is negative, the result is machine dependent. i.e. 6/7=0 and -6/-7=0 But, -6/7 = 0 or 1.(machine dependent)
10
Real Arithmetic An arithmetic involving only real
operands is called real arithmetic. A real operand may can have values either in decimal notation or in exponent notation. Since floating point values are rounded to the number of significant digits permissible, the final value is an approximation of the correct result.
11
The operator modulo(%) cannot be used with real operands.
i.e, 6.0/7.0 = -2.0/3.0 = If a is integer type ,then a=6.0/7.0 printf(“value of a is:%d”,a); O/P:- Value of a is:0 The operator modulo(%) cannot be used with real operands.
12
Mixed-mode Arithmetic
When one operand is real and the other is integer, the expression is called a mixed-mode arithmetic expression. If either operand is of real type, then only the real operation is performed and the result is always a real number.
13
i.e., / 10.0 = 1.5 where as, 15 / 10 = 1
14
Relational Operators In ‘C we are having relational operator for comparing different relations. i.e. 4<5, 7>3. Any relational expression can have either true (non-zero) or false (zero) value. I.e <5 is true…. Non-zero 4>5 is false…. Zero value.
15
Different relational operators
< is less than <= is less than or equal to > is greater than >= is greater than or equal to == is equal to != is not equal to
16
General general form:- a_exp1 relational_operator a_exp2
Like, < 9+9 When arithmetic expressions are used on either side of a relational operator, the arithmetic operators have a higher priority over relational operators.
17
It means first both side’s a_expressions are
evaluated and then the results are compared. So, < 9+9 12 < ….. True… non-zero 17
18
Logical Operators && logical AND || logical OR ! logical NOT
Logical Operators are used when we want to test more than one condition and make decisions.
19
Logical Operators OP-1 OP-2 OP1&&OP2 (And) OP1||OP2 (Or) Non-Zero 1
20
Examples:- Where we can use such expressions? condition like,
if (age>50 && salary<1000) if (number<0 || number>100) later on…………. in short, a>b && x==10
21
Assignment operators Assignment operators are used to
assign the result of an expression to a variable. Assignment operator is ‘=‘ What is the difference between ‘=‘ and ‘==‘ operators? i.e i=5; if (i==5)
22
Shorthand assignment operators
X += Y; is same as X = X+Y; Means, Variable OP= exp; Variable = Variable OP (exp);
23
Shorthand assignment operators
Statement with simple assignment operator Statement with shorthand assignment operator a=a+1 a+=1 a=a-1 a-=1 a=a*(n+1) a*=n+1 a=a/(n+1) a/=n+1 a=a % b a%=b
24
Advantage of Shorthand assignment operators
Easier to read and write. More efficient. e.g. value(2*j-23) = value(2*j-23) + y; With s.a.o:- value(2*j-23) + = y; Expression 2*j-23 is evaluated only once
25
Increment and decrement operator
The operator ++ adds 1 to the operand While -- subtracts 1 to the operand i.e. int a=9; a++; so now a is 10. because 1 added to a. a postfix ++ ++a prefix ++ Both have same effect while they form statements independently. BUT…
26
Difference prefix and postfix
X=a++; and x=++a; is different….. Here in x = a++; The value of a is assigned to x then a is incremented Means, if a=9 then x will have 9 and then a get incremented so a becomes 10.
27
While, x=++a; Then first increment so, a is now 10… and the value is assigned after so x is also 10. e.g. int i,j,k; i=10; j=i++; k=++i; Now, what is the value of i,j,k????
28
Conditional operator the operator ?: is ternary operator
SYNTAX: Exp1?exp2:exp3; a=10; b=15; x = (a>b) ? a : b ; here, exp1 a>b is evaluated first
29
If it is non-zero (true) then,
exp2 is evaluated and assigned to LHS. Otherwise exp3 is evaluated and assigned to LHS. In Simple way, if (a>b) x = a; else x = b;
30
Bitwise operator Operator meaning
for manipulation of data at bit level Operator meaning & bitwise AND | bitwise OR ^ bitwise exclusive OR << Shift left >> Shift right ~ One’s complement
31
The comma operator value = (x=20, y=5, x+y);
Comma-linked list of expressions are evaluated left to right…and the value of right most expression is the value of the combined expression. Here above, x will get 20 then y will get 5 then expression x+y is evaluated as 25 and that is the value of ( … ) , so given to value. So here value contains 25.
32
sizeof operator when used with operand, it returns the no. of bytes the operand occupies… i.e. sizeof(int) is returns 2 sizeof(char) returns 1 ..on 16bit processor. Q. float x; printf(“%d”,sizeof(x)); what will be the output? Why?
33
Precedence of arithmetic operator
High priority * / % Low priority The basic evaluation includes left to right passes….. During first pass higher priority opr is applied as they encounter….during second pass from left to right the low priority operators are applied as they are encountered.
34
example i.e x = 9-12/3+3*2-1; pass 1: x = 9 – 4 + 3*2 –1;
35
Use of parenthesis to override normal precedence
X= 9 – 12 / (3+3) *(2-1) Pass 0: x=9 – 12 / 6 * (2-1) x=9 – 12 / 6 * 1; Pass 1: x=9 – 2 * 1; x=9 – 2; Pass 2: x=7 Thus, when parenthesis are used, the expressions within parenthesis assume highest priority.
36
Associativity The operator having same precedence are evaluated either from left to right or right to left…..depending of the level … .known as associativity.
37
Type Conversions:- There are two ways for type conversion.
Implicit Type Conversion Explicit Type Conversion
38
Implicit Type Conversion
Automatic conversion. e.g. a=3 b=2 int i; float a,b; i=a/b; here, value of i will be 1. instead of 1.5
39
Explicit Type Conversion
(type-name) expression Where, type-name is one of the standard C data types. Expression may be a constant, variable or an expression.
40
Example i=( int ) 7.5 Means, 7.5 is converted to integer by
truncation. i=( int )21.3/ ( int )4.5 Means, Evaluated as 21/4 and result would be 5.
41
Casting can be used to round-off a given value.
HOW??? x = (int) (y+0.5); if y = then, y+0.5=28.1 and on casting the result becomes 28.
42
X= (a++) – (--b) + (c--) – (--a); Y= (x--) + (++a) – (--b) + (c++);
EXAMPLE:- Value of a=5,b=10,c=15 X= (a++) – (--b) + (c--) – (--a); Y= (x--) + (++a) – (--b) + (c++); Find the value of X,Y????
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.