开发者

About associativity in 'C'

开发者 https://www.devze.com 2023-01-04 11:01 出处:网络
Ho开发者_JAVA技巧w can i decide or understand whether any statement or expression has left to right or right to left associativity?=, +=, etc, operators that have \'=\' in them, is right associated.

Ho开发者_JAVA技巧w can i decide or understand whether any statement or expression has left to right or right to left associativity?


=, +=, etc, operators that have '=' in them, is right associated.

All others are left-associated.


Here's a table of C++ operator precedence.

The operator precedence and associativity are the same in C and C++ (modulo operators that don't exist in the former).


Read the standards

EDITED: I don't have the standards myself, but I've found this link supposed to be based on the standard.

Also, quoting Kernighan & Ritchie:

Arithmetic operators associate left to right.

Expressions connected by && or || are evaluated left to right, and evaluation stops as soon as the truth or falsehood of the result is known. Most C programs rely on these properties.

Operators Associativity

() [] -> . left to right

! ~ ++ -- + - * (type) 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

Unary & +, -, and * have higher precedence than the binary forms.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号