Loop Control Statements in C Introduction to HTML How to use the Github API The image tag, anchor tag and the button tag Ordered and Unordered Lists in HTML The division tag HTML Forms Tables in HTML Introduction to C Programming Introduction to Python Varibles and Datatypes in Python Operators in Python Typecasting in Python Input and Output in Python If Else in Python Loops in Python Break, Continue and Pass in Python Python practice section 1 Lists in Python Tuple in Python

Precedence and Associativity of Operators

When multiple operators are used in one expression such as ++,-- or + or *, etc we need to decide the order in which to evaluate different operators on the basis of priority in order to get the correct results. This is called precedence of operators.
Associativity means that when there are multiple operators of the same kind for e.g - a*b*c or a+b+c etc. then the direction in which to evaluate the expression - left to right or right to left.

C Operator Precedence and Associativity Table

Operator Associativity Precedence
() [] -> . Left to right Highest
++ -- + - ! ~ (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 Lowest

Example


#include<stdio.h>
void main()
{
    int a=19,b=10,c=40,d=20,e=2;

    // This expression will be evaluated as (a + (b * (c/d) ) ) based on precedence of operators.

    printf("Value of a+b*c/d = %d \n",a+b*c/d);

    // Brackets have the highest priority, this expression will be evaluated as 
    //(((++a) + ((b*c)/(d--)))+(--e) )

    printf("Value of (++a+(b*c)/d--)+--e = %d \n",(++a+(b*c)/d--)+--e);
}
     

Output

Value of a+b*c/d = 39 
Value of (++a+(b*c)/d--)+--e = 41