Define
the terms precedence and associativity.
Answer:
When more
than one operator is used in an expression, C language has predefined rules of
to evaluate expression based on priority of operators. This rule of priority
operators is known as precedence of operators.
In
arithmetic operators,
Operator
|
Priority
|
Associativity
|
*, /, %
|
First
|
Left to Right
|
+, -
|
Second
|
Left to Right
|
Associativity:
If operators
have same priority in one expression then rules define which operator is
executed first in which order. This rule is known as associativity.
For
example:
If
expression contains * and / operators then both have same priority, so
associativity rules will be applied for evaluating expression.
Ex:
D = A + B – C.
In this +
and – both have same priority
Consider
following table for both priority (precedence) and associativity. In this table
top most operator has highest priority.
Operator
|
Meaning
|
Associativity
|
( ), ++, -
-
|
Parenthesis,
postfix increment, postfix decrement
|
Left to
right
|
++, - -,
sizeof
|
Prefix
increment, prefix decrement, sizeof operator
|
Right to
left
|
*, /, %
|
Multiplication,
division, modulus
|
Left to
right
|
+, -
|
Addition,
subtraction
|
Left to
right
|
No comments:
Post a Comment