The Priority or Precedence of operator is the order in which the operators are to be evaluated, written in a expression is called the Hierarchy of operators.
The associativity specifies the operator's direction to be performed, it may be left-to-right or it may be right-to-left.
Rank | Operator | Description | Associativity |
---|---|---|---|
( ) | Parentheses | left-to-right | |
++ , -- | Pre-increment/pre-decrement | right-to-left | |
sizeof | Determine size in bytes | right-to-left | |
* , / , % | Multiplication, division, modulus | left-to-right | |
+ , - | Addition, subtraction | left-to-right | |
< , > , <= , >= | Relational less than, less than or equals to, greater than, greater than or equals to | left-to-right | |
== , != | Relational is equal to/is not equal to | left-to-right | |
++ , -- | Post-increment/post-decrement | right-to-left | |
&& | Logical AND | left-to-right | |
|| | Logical OR | left-to-right | |
?: | Ternary conditional | right-to-left | |
= -= += >= <= | Assignment | right-to-left |
Example 1: Program to explain operator precedence.
#include<stdio.h>
void main()
{
float r;
r = 1.0 + 2.0 * 3.0 / 4.0;
printf("\n%f",r);
r = 1.0 / 2.0 + 3.0;
printf("\n%f",r);
r = (1.0 + 2.0) / 3.0;
printf("\n%f",r);
r = (1.0 + 2.0 / 3.0) + 4.0;
printf("\n%f",r);
}