Sunday 13 November 2011

Does operator precedence always work (left to right, right to left)? in C programming

Does operator precedence always work (left to right, right to left)?

If you mean “Does a right-to-left precedence operator ever go left to right, and vice versa?” the answer
is no. If you mean “Can a lower-order precedence ever be risen above a higher-order precedence?” the
answer is yes. Table XIV.9 lists the order of each operator from top to bottom (highest order to lowest) and
shows each operator’s associativity

Operator precedence.

Operator                                         Associativity
() [] -> .                                         Left to right
! ~ ++  -- - (typecast) * & sizeof       Right to left
* / %                                              Left to right
+ -                                                  Left to right
<< >>                                             Left to right
< <= > >=                                       Left to right
== !=                                              Left to right
&                                                   Left to right
^                                                    Left to right
|                                                     Left to right
&&                                                Left to right
||                                                    Left to right
?:                                                   Right to left
= += -=                                          Right to left
,                                                     Left to right

Note in the table that the != operator takes precedence over the = operator (in fact, practically everything takes precedence over the = operator). The following two source lines illustrate how precedence of one operator over another can get a programmer into trouble:

while(ch = getch() != 27) printf(“Got a character\n”);
while((ch = getch()) != 27) printf(“Got a character\n”);

Obviously, the purpose of this code is to get a character from the keyboard and check it against decimal 27 (the Escape key). Unfortunately, in source line one, the getch() is compared to the Escape key. The resulting test (which will return a TRUE or FALSE), not the character from the keyboard, is placed into ch. This is due to the precedence of the != operator over the = operator. In the second source line, a set of parentheses was added to surround the ch = getch() operation. Because parentheses are the highest order of precedence, the keyboard character is placed into ch, then checked against the Escape key. This final check will return TRUE or FALSE to the while statement, which is exactly what isdesired (while this statement is TRUE, print this sentence). As a matter of detail, it should be pointed out thatch is not checked against 27; the result of the parenthetical statement ch = getch() is checked against 27. It might not make much difference in this case, but parentheses can really change the way code is createdand executed. In the case of statements with multiple parenthetical statements, the code is executed from theinnermost set of parentheses to the outermost, from left to right.

Note
that the associativity in each operator’s individual case (left to right or right to left) does not change, but the order of precedence does.

Cross Reference:

None.

No comments:

Post a Comment