We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF or read online on Scribd
You are on page 1/ 2
Operators and Express
1D fas
in C has a precedence associated with
ine how an expression involving more. ened
his precedence is used to deter
it re P operator is evaluated. Thore are distinct levels of procedonce ‘andan | Discuss how operator
than Stor may belong to one of these levels. The operators al the higher lever | Precedence and
oporamedence are evaluated first. The operators of the same precedenes ene ee tee arel
,
ig known as the associativly property of an operator. Table 4 8 provides
Tn plete lst of operators, their precedence levels, and their rules of asseciation, The
a order of decreasing precedence. Rank 1 indicates the highest precedence level ang 1
tne emncludes those operators, which we Nave not yel been discussed
a cis very important to note carefully, the order of precedence and associativity of operators. Consider the
following conditional statement:
groups are listed in
5 the lowest. The list
if (x == 10 + 15 && y < 10)
The precedence rules say that the addition operator has a higher priority than the logical operator (&&) and
the relational operators ( == and <). Therefore, the addition of 10 and 15 is executed first. This is equivalent to
if (x == 25 && y < 10)
The next step is to determine whether x is equal to 25 and y is less than 10. If we assume a value of 20
for x and 5 for y, then
Jable 4.8 Summary of C Operators
Goce PEs
[Ese
Function call Left to right
Aray element reference
Unary plus
Unary minus Right to left 2
Increment
Decrement
Logical negation
Ones complement
Pointer reference (indirection)
Address
Size of an object
‘Type cast (conversion)
Multiplication Left to right
Division
Modulus
‘Addition Left te right
Subtraction
Left shit Left to right
Right shift
Less than Loft to right
Less than or equal to
Greater than
(Conta.)94, Programming in ANSI C _ il
TABLE 4.8 (Contd.)
ices Pare
Greater than or equal to
Equality
Inequality
Bitwise AND
CEST
Leff to right
Left to right
Bitwise XOR Left to right
Bitwise OR Left to right
Logical AND Left to right
Logical OR Left to right
Conditional expression Right to left
Assignment operators Right to left
Comma operator Leftto right
5 is FALSE (0)
¥ < 10 is TRUE (1)
Note that since the operator < enjoys a higher priority compared t
25 is tested.
Finally we get:
+ < 10s tested first and then x
if (FALSE && TRUE)
Because one of the conditions is FALSE, the complex condition is FALSE.
In the case of & it s guaranteed that the second operand will not be evaluated if the first is zero and in
{he case of |, the second operand will not be evaluated ifthe first is Non-zero,
Rules of Precedence and Associativity
* Precedence rules decides the order in which different operators are applied
* Associativily rule decides the order in which multiple occurrences of the an.