§ C
supports a rich set of built-in operators.
§
An operator is a symbol that tells the
computer to perform certain mathematical or logical manipulations.
§ C
operators can be classified into 8 categories.
Arithmetic
Operators
§ C
provides all the basic arithmetic operators
Operator
|
Meaning
|
+
|
Addition
|
-
|
Subtraction
|
*
|
Multiplication
|
/
|
Division
|
%
|
Modulo
division
|
NOTE: %
cannot be applied to float data values
§
Three different type of calculations can
be carried out:
i.
Integer arithmetic
ii.
Real arithmetic
iii.
Mixed-mode arithmetic: When one operand
is integer and other floating point, the result is always floating point
because integer gets promoted to float first and then the expression is
evaluated.
Relational
Operators
§ We
often compare two quantities and depending on their relation, take certain
decisions. These comparisons can be done with the help of relational operators.
Operator
|
Meaning
|
<
|
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
|
Logical
Operators
§ C has the following three logical
operators
&&
meaning logical AND
|| meaning logical OR
! meaning logical NOT
Truth
Table
OP-1
|
OP-2
|
OP-1
&& OP-2
|
OP-1
|| OP-2
|
!OP-1
|
1
|
1
|
1
|
1
|
0
|
1
|
0
|
0
|
1
|
-
|
0
|
1
|
0
|
1
|
1
|
0
|
0
|
0
|
0
|
-
|
Assignment
Operator
§ Assignment
operator is used to assign the result of an expression to a variable.
§
Syntax:
variable_name
= expression;
§
C also has a set of ‘shorthand’
assignment operator of the form:
Syntax:
v op = exp;
where, v is a
variable
op is a arithmetic operator
exp is an expression
Statement with simple assignment
operator
|
Statement with shorthand 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
|
Increment and
Decrement Operators
§ Increment
and decrement operators are unary operators and they require variable as their
operands.
§
These operators are solved right to
left.
§
When postfix ++ (or --) is used with a
variable in an expression, the expression is evaluated first using the original
value of the variable and then the variable is incremented (or decremented) by
one.
§
When prefix ++ (or --) is used in an
expression, the variable is incremented (or decremented) first and then the
expression is evaluated using the new value of the variable.
§ The
precedence and associativity of ++ and – operators are the same as those of
unary + and unary -.
Conditional
Operator
§ A
ternary operator pair’?:’ is available in C to construct conditional
expressions of the form:
exp1
? exp2 : exp3;
where exp1, exp2, exp3 are
expressions.
Working
exp1 is
evaluated first. If it is nonzero (true), then the expression exp2 is evaluated
and becomes the value of the expression. If exp1 is false, exp3 is evaluated
and its value becomes the value of the expression.
e.g.
a =
10;
b =
15;
x =
(a > b) ? a : b;
Bitwise
Operators
§ C has
a distinction of supporting special operators known as bitwise operators for
manipulation of data at bit level.
§
These operators are used for testing of
bits, or shifting them right or left.
Operator
|
Meaning
|
&
|
bitwise
AND
|
|
|
Bitwise
OR
|
^
|
bitwise
exclusive OR
|
<<
|
Shift
Left
|
>>
|
Shift
Right
|
Special
Operators
a)
Comma
Operator
§ The
comma operator can be used to link the related expressions together.
§ A
comma-linked list of expressions are evaluated left to right and the value of
right-most expression is the value of the combined expressions.
e.g. value = (x = 10,
y = 5, x + y);
b) sizeof Operator
§ the
sizeof is a compile time operator and whn used with an operand, it returns the
number of bytes the operator occupies.
§ The
operand may be a variable, a constant or a data type qualifier.
e.g. m = sizeof (sum);
m = sizeof (long int);
§ The
sizeof operator is normally used to determine the lengths of array and
structures when their sizes are not known to the programmer.
§ It is
also used to allocate memory space dynamically to variables during the
execution of a program.
PRECEDENCE OF
ARITHMETIC OPERATORS
§ An arithmetic
expression without parentheses will be evaluated from left to right using the
rules of precedence of operators.
§
There are two distinct priority levels
of arithmetic operators in C:
High
priority * / %
Low
priority + -
§ The
basic evaluation procedure include ‘two’ left-to-right passes through the
expression.
During
the first pass, the high priority operators (if any) are applied as they are
encountered.
During
the second pass, the low priority operators (if any) are applied as they are
encountered.
e.g.
x = 9 - 12 / 3 + 3 * 2 – 1
First Pass:
Step 1: x = 9 –
4 + 3 * 2 – 1
Step 2: x = 9 –
4 + 6 – 1
Second Pass:
Step 3: x = 5 + 6 – 1
Step 4: x = 11 – 1
Step 5: x = 10
TYPE CONVERSION
IN EXPRESSIONS
§ If
the operands are of different types, the lower type is automatically converted
to the higher type before the operation proceeds. The result is of higher type.
§
Conversion
Hierarchy
Fig 06: Conversion
Hierarchy
§ Implicit Type Conversion
C permits mixing
of constants and variables of different types in an expression. C automatically
converts any intermediate values to the proper type so that the expression can
be evaluated without losing any significance. This automatic conversion is
known as implicit type conversion.
§ Explicit Type Conversion
The process of
local conversion is known as explicit conversion or casting a value. The
general form of cast is:
(data type) expression;
e.g.
The calculation
of ratio of females to males in a town is given by the formula:
ratio
= female_number / male_number;
Since
female_number and male_number are declared as integers in the program, the
decimal part of the result of division would be lost and ratio would represent
a wrong number. This problem can be solved by converting locally one of the
variables to the floating point as shown below:
ratio = (float) female_number / male_number;
ratio = (float) female_number / male_number;
The operator (float) converts the
female_number to floating point for the purpose of evaluation of the
expression. Then using the rule of automatic conversion, the division is
performed in floating point mode, thus returning the fractional part of the
result.
OPERATOR
PRECEDENCE and ASSOCIATIVITY
§ Precedence is
defined as the priority assigned to operators such that it would be clear that
which operator should be applied first.
The operator
with higher priority should be given priority over the operator with lower priority.
§ Associativity defines
which operator will work and how it will work.
OPERATOR
|
DESCRIPTION
|
ASSOCIATIVITY
|
RANK
|
( )
[ ]
|
Function
Call
Array
element reference
|
Left to Right
|
1
|
+
-
++
--
!
~
*
&
sizeof
(type)
|
Unary
plus
Unary
minus
Increment
Decrement
Logical
negation
One’s
complement
Pointer
reference
Address
Size
of an object
Typecast(conversion)
|
Right to Left
|
2
|
*
/
%
|
Multiplication
Division
Modulus
|
Left to Right
|
3
|
+
-
|
Addition
Subtraction
|
Left to Right
|
4
|
<<
>>
|
Left
Shift
Right
Shift
|
Left to Right
|
5
|
<
< =
>
> =
|
Less
than
Less
than or equal to
Greater
than
Greater
than or equal to
|
Left to Right
|
6
|
= =
!=
|
Equality
Inequality
|
Left to Right
|
7
|
&
|
Bitwise
AND
|
Left to Right
|
8
|
^
|
Bitwise
XOR
|
Left to Right
|
9
|
|
|
Bitwise
OR
|
Left to Right
|
10
|
&&
|
Logical
AND
|
Left to Right
|
11
|
||
|
Logical
OR
|
Left to Right
|
12
|
?:
|
Conditional
Operator
|
Right to Left
|
13
|
=
*=
/= %=
+= -=
&=
^=
|=
<<= >>=
|
Assignment
Operator
|
Right to Left
|
14
|
,
|
Comma
Operator
|
Left to Right
|
15
|
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.