Uploaded by cse225092brur

Basics of Operators

advertisement
Operators
By , Ramjan Hossain Noor
CSE,BRUR
14 th intake
AN OPERATOR IS A SYMBOL THAT TELL THE
COMPUTER TO PERFORM CERTAIN
MATHEMATICAL OR LOGICAL
MANIPULATION.
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
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.
Arithmetic Operators
Operator
Meaning
+
Addition or unary plus
-
Subtraction or unarminus
*
Multiplication
/
Division
%
Modulo division (reminder)
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.
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)
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
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)
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.
i.e, 6.0/7.0 =0.857143
-2.0/3.0 = -0.666667
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.
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.
i.e.
15 / 10.0 = 1.5
Where As,
15 / 10 = 1
Relational Operators
In ‘C we are having relational operator for c
comparing different relations.
i.e. 4<5, 7>3.
Any relational expression can have either true (nonzero) or false (zero) value.
I.e.
4<5 is true…. Non-zero
4>5 is false…. Zero value.
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
General
general form:a_exp1 relational_operator
Like,
a_exp2
5+7 < 9+9
When arithmetic expressions are used on
either side of a relational operator,
the arithmetic operators have a higher
priority over relational operators.
It means first both side’s a_expressions are
evaluated and then the results are
compared.
So,
5+7 < 9+9
12 < 18
….. True… non-zero
Logical Operators
&&
LOGICAL AND
||
LOGICAL OR
! LOGICAL NOT
❑
LOGICAL OPERATORS ARE USED WHEN WE
WANT TO TEST MORE THAN ONE CONDITION
AND MAKE DECISIONS.
Logical Operators
OP-1
OP-2
OP1&&OP2
(And)
OP1||OP2
(Or)
Non-Zero
Non-Zero
1
1
Non-Zero
0
0
1
0
Non-Zero
0
1
0
0
0
0
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
Assignment operators
ASSIGNMENT OPERATORS ARE USED TO
ASSIGN THE RESULT OF AN EXPRESSION TO A
VARIABLE.
ASSIGNMENT OPERATOR IS ‘=‘
Q
WHAT IS THE DIFFERENCE BETWEEN ‘=‘ AND ‘==‘
OPERATORS?
I.E I=5;
IF (I==5)
Shorthand assignment
operators
X += Y;
IS SAME AS
X = X+Y;
MEANS,
VARIABLE OP= EXP;
NB. OP =OPERATOR
IS SAME AS
VARIABLE = VARIABLE OP (EXP);
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
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
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…
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.
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????
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
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;
Bitwise operator
FOR MANIPULATION OF DATA AT BIT LEVEL:
OPERATOR
MEANING
&
BITWISE AND
|
BITWISE OR
^
BITWISE EXCLUSIVE OR
<<
SHIFT LEFT
>>
SHIFT RIGHT
~
ONE’S COMPLEMENT
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.
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?
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.
Example
i.e
x = 9-12/3+3*2-1;
Pass 1: x = 9 – 4 + 3*2 –1;
x = 9 – 4 + 6 –1;
Pass 2:
x= 5 +6 –1;
x=11-1;
x=10;
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.
Associativity
THE OPERATOR HAVING SAME PRECEDENCE ARE
EVALUATED EITHER FROM LEFT TO RIGHT OR
RIGHT TO LEFT…..DEPENDING OF THE LEVEL …
KNOWN AS ASSOCIATIVITY.
Type Conversions:❑
THERE ARE TWO WAYS FOR TYPE CONVERSION.
1)
IMPLICIT TYPE CONVERSION
1)
EXPLICIT TYPE CONVERSION
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
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.
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.
❑
CASTING CAN BE USED TO ROUND-OFF A GIVEN
VALUE. HOW ?
X = (INT) (Y+0.5);
IF Y = 27.6
THEN,
Y+0.5=28.1 AND ON CASTING THE RESULT
BECOMES 28.
Examples
VALUE OF A=5, B=10, C=15
X= (A++) – (--B) + (C--) – (--A);
Y= (X--) + (++A) – (--B) + (C++);
Q. FIND THE VALUE OF X,Y .
Download