What is C Operator Precedence and Associativity?

CServer Side ProgrammingProgramming

First, let us understand what is operator precedence in C programming language.

Operator Precedence

Operator precedence is used to evaluate the order of operators evaluated in an expression. In C programming, every operator has a priority. When there is more than one operator in the given expression, the operator with higher precedence or priority is evaluated first and the operator with the least priority is evaluated later.

Operator Associativity

Operator associativity is used to evaluate the order of operators with equal precedence in an expression. In the C programming language, when an expression contains multiple operators with equal or same precedence, we use associativity to determine the order of evaluation of operators.

The operators and their precedence and associativity is explained in the table given below −

PrecedenceOperatorOperator MeaningAssociativity
1()
[]
->
.
Function Call
array reference
Structure member access
Structure member access
Left to Right
2!
~
+
-
++
--
&
*
sizeof(type)
negation
1's complement
Unary Plus
Unary minus
Increment operator
decrement operator
address of operator
pointer
returns size of a variable type conversion
Right to Left
3*
/
%
multiplication
division
remainder
Left to Right
4+
-
addition
subtraction
Left to Right
5<<
>>
left shift
right shift
Left to Right
6<
<=
less than
less than or equal to
Left to Right
7==
!=
equal to
not equal to
Left to Right
8&bitwise ANDLeft to Right
9^bitwise EXCLUSIVE ORLeft to Right
10|bitwise ORLeft to Right
11&&logical ANDLeft to Right
12||logical ORLeft to Right
13?:Conditional OperatorLeft to Right
14=
*=
/=
%=
+=
-=
&=
^=
|=
<<=
>>=
assignment
assign multiplication
assign division
assign remainder
assign addition
assign subtraction
assign bitwise AND
assign bitwise XOR
assign bitwise OR
assign left shift
assign right shif
Right to Left
15
separatorLeft to Right


raja
Published on 03-Sep-2021 06:30:49
Advertisements