Operator Precedence in C Language

Operator Precedence

If an expression has more than one operator, it is important to know the order in which they will be applied. the hierarchy of operators precedence is given below.

C language has a predefined rule of priority for the operators. This rule of priority of operators is called operators precedence.

For example, the expression a=4+b*2 contains two operators, an addition and a multiplication. C compiler evaluates 4+b first, then multiply the result by 2, or it evaluates b*2 first, then adds 4 to the result?

Operators Description Associativity
++ -- Post increment/post decrement operator Left to right associativity
++ -- Pre-increment/pre-decrement operator Right to left associativity
+ - Unary plus/minus operator Right to left associativity
! ~ Logical NOT/bitwise NOT operator Right to left associativity
* "Value at address" operator Right to left associativity
& "Address of" operator Right to left associativity
sizeof "Size of" operator Right to left associativity
* / % Multiplication/division/modulus operator Left to right associativity
+ - Addition/subtraction operator Left to right associativity
<< >> Bitwise left shift/bitwise right shift operator Left to right associativity
< <= "Less than"/"Less than or equal to" relational operator Left to right associativity
> >= "Greater than"/"Greater than or equal to" relational operator Left to right associativity
== != "Equal to"/"Not equal to" relational operator Left to right associativity
& Bitwise AND operator Left to right associativity
^ Bitwise XOR operator Left to right associativity
| Bitwise OR operator Left to right associativity
&& Logical AND operator Left to right associativity
|| Logical OR operator Left to right associativity
?: Ternary conditional operator Right to left associativity
= Assignment operator Right to left associativity
+= -= Assignment with addition/subtraction Right to left associativity
*= /= %/ Assignment with multiplication/division/modulus Right to left associativity
<<= >>= Assignment with bitwise left shift/bitwise right shift Right to left associativity
&= ^= |= Assignment with bitwise AND/bitwise XOR/bitwise OR Right to left associativity

Example:



Output:

 Value of a + b * c / d is    : 25.000000
 Value of (a + b) * c / d is  : 22.500000
 Value of a + (b * c) / d is  : 25.000000
 Value of a + b * (c / d) is  : 25.000000