C

Associativity And Operator Precedence In The C Programming Language

This guide will teach you about operator precedence and associativity in C programming.
C Operator Precedence
When an expression contains more than one operator, operator precedence determines which operator is evaluated first. For example, 100-2*30 would return 40 because it is calculated as 100 – (2*30) rather than (100-2)*30. The reason for this is that multiplication * takes precedence over subtraction (-).
Associativity in C

Associativity is used when an expression contains two or more operators with the same precedence. For example, multiplication and division arithmetic operators have the same precedence. If we have an expression 5*2/10, it will be evaluated as (5*2)/10 because the associativity for these operators is left to right. Likewise, 20/2*5 is calculated as (20*2)/5.

In C programming, operator precedence and the Associativity Table are used.

Description Operator Associativity
Function expression

( )

Left to Right
Array Expression

[]

Left to Right
Structure operators

->

Left to Right
Unary minus

Right to Left
Increment & Decrement

—  ++

Right to Left
One’s compliment

~

Right to Left
Pointer Operators

& *

Right to Left
Type cast

(data type)

Right to Left
size of operator

sizeof

Right to Left
Left and Right Shift

>>  <<

Operators for Arithmetic

Multiplication operator, Divide by, Modulus

*,  /,  %

Left to Right
Add, Substract

+,  –

Left to Right

Operators for Relationships

Less Than

<

Left to Right
Greater than

>

Left to Right
Less than equal to

<=

Left to Right
Greater than equal to

>=

Left to Right
Equal to

==

Left to Right
Not equal

!=

Left to Right

Operators Logical

AND

&&

Left to Right
OR

||

Left to Right
NOT

!

Right to Left

Bitwise Functions

AND

&

Left to Right
Exclusive OR

^

Left to Right
Inclusive OR

|

Left to Right

Operators of Assignment

=

Right to Left

*=

Right to Left

/=

Right to Left

%=

Right to Left

+=

Right to Left

-=

Right to Left

&=

Right to Left

^=

Right to Left

|=

Right to Left

<<=

Right to Left

>>=

Right to Left

Additional Operators

Comma

,

Right to Left
Operator with a condition

?:

Right to Left

 

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button