When two or more operators share an operand the operator with the higher precedence goes first. For an example, 1 + 2 * 3 is treated as 1 + (2 * 3), because multiplication has a higher precedence than addition.
Operator Precedence and Associativity
Priority | Category | Operator | What is it (or does) | Associativity |
1 | Highest | ( ) [ ] -> . |
Function call Array subscript Indirect component selector Direct component selector |
Left to Right |
2 | Unary | ! ~ + - ++ -- & * sizeof (type) |
Logical negation (NOT) Bitwise (1's) complement Unary plus Unary minus Preincrement or postincrement Predecrement or postdecrement Address Indirection Size of operand, in bytes TypeCast |
Right to Left |
3 | Multiplicative | * / % |
Multiply Divide Remainder (modulus) |
Left to Right |
4 | Aditive | + - |
Binary plus Binary minus |
Left to Right |
5 | Shift | << >> |
Shift left Shift right |
Left to Right |
6 | Relational | < <= > >= |
Less than Less than or equal to Greater than Greater than or equal to |
Left to Right |
7 | Equality | == != |
Equal to Not equal to |
Left to Right |
8 | Bitwise AND | & | Bitwise AND | Left to Right |
9 | Bitwise XOR | ^ | Bitwise XOR | Left to Right |
10 | Bitwise OR | | | Bitwise OR | Left to Right |
11 | Logical AND | && | Logical AND | Left to Right |
12 | Logical OR | || | Logical OR | Left to Right |
13 | Conditional | ?: | "a ? x : y" means "if a then x, else y" | Right to Left |
14 | Assignment | = *= /= %= += -= &= ^= |= <<= >>= |
Simple assignment Assign product Assign quotient Assign remainder (modulus) Assign sum Assign difference Assign bitwise AND Assign bitwise XOR Assign bitwise OR Assign left shift Assign right shift |
Right to Left |
15 | Comma | , | Evaluate | Left to Right |
Example for Operatro precedence
#include <stdio.h>
int main(){
float a=1,b=2,c=3,d;
d=a+b*c;
printf("a+b*c=%f\n",d);
d=(a+b)*c;
printf("(a+b)*c=%f",a);
return 0;
}
Output
a+b*c=7.000000(a+b)*c=1.000000