- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Operator Precedence and Associativity in C

Operator precedence determines the grouping of terms in an expression and decides how an expression is evaluated. Certain operators have higher precedence than others; for example, the multiplication operator has a higher precedence than the addition operator.

For example, x = 7 + 3 * 2; here, x is assigned 13, not 20 because operator * has a higher precedence than +, so it first gets multiplied with 3*2 and then adds into 7.

Here, operators with the highest precedence appear at the top of the table, those with the lowest appear at the bottom. Within an expression, higher precedence operators will be evaluated first.

Category | Operator | Associativity |
---|---|---|

Postfix | () [] -> . ++ - - | Left to right |

Unary | + - ! ~ ++ - - (type)* & sizeof | Right to left |

Multiplicative | * / % | Left to right |

Additive | + - | Left to right |

Shift | << >> | Left to right |

Relational | < <= > >= | Left to right |

Equality | == != | Left to right |

Bitwise AND | & | Left to right |

Bitwise XOR | ^ | Left to right |

Bitwise OR | | | Left to right |

Logical AND | && | Left to right |

Logical OR | || | Left to right |

Conditional | ?: | Right to left |

Assignment | = += -= *= /= %=>>= <<= &= ^= |= | Right to left |

Comma | , | Left to right |

## Example Code

#include <stdio.h> main() { int a = 20; int b = 10; int c = 15; int d = 5; int e; e = (a + b) * c / d; // ( 30 * 15 ) / 5 printf("Value of (a + b) * c / d is : %d

", e ); e = ((a + b) * c) / d; // (30 * 15 ) / 5 printf("Value of ((a + b) * c) / d is : %d

" , e ); e = (a + b) * (c / d); // (30) * (15/5) printf("Value of (a + b) * (c / d) is : %d

", e ); e = a + (b * c) / d; // 20 + (150/5) printf("Value of a + (b * c) / d is : %d

" , e ); return 0; }

## Output

Value of (a + b) * c / d is : 90 Value of ((a + b) * c) / d is : 90 Value of (a + b) * (c / d) is : 90 Value of a + (b * c) / d is : 50

- Related Questions & Answers
- What is C Operator Precedence and Associativity?
- C++ Operators with Precedence and Associativity
- PHP Operator Precedence
- What is the operator precedence in C#?
- According to Java Operator precedence, which operator has the highest precedence?
- Explain Operator grammar and precedence parser in TOC
- What is Operator Precedence Parsing?
- Can we change operator precedence in Python?
- What are Precedence Relations in Operator Grammar?
- Floating Point Operations and Associativity in C, C++ and Java
- What is the associativity of Python's ** operator?
- What are LEADING and TRAILING operation of an operator precedence grammar?
- Precedence of postfix ++ and prefix ++ in C/C++
- How Can MySQL operator precedence affect result set?
- Evaluating a mathematical expression considering Operator Precedence in JavaScript

Advertisements