Operator precedence tells the computer which math or logic steps to do first when there are many in one line.
0
0
Operator precedence
Introduction
When you write math calculations with multiple operators like +, -, *, / in one line.
When you combine different types of operations like adding and multiplying in the same expression.
When you use logical operators like && and || together in a condition.
When you want to make sure your code does things in the right order without extra parentheses.
When you want to avoid mistakes in complex expressions by understanding which parts run first.
Syntax
C
expression with multiple operators, e.g., a + b * c - d / e
Operators with higher precedence run before operators with lower precedence.
Use parentheses () to change the order if needed.
Examples
Multiplication (*) happens before addition (+), so 3*4=12 first, then 2+12=14.
C
int result = 2 + 3 * 4;
Parentheses force addition first: 2+3=5, then multiply by 4 = 20.
C
int result = (2 + 3) * 4;
Division (/) happens before addition (+), so 10/2=5, then 5+3=8.
C
int result = 10 / 2 + 3;
Parentheses change order: 2+3=5, then 10/5=2.
C
int result = 10 / (2 + 3);
Sample Program
This program shows how operator precedence works with multiplication, division, addition, and parentheses.
C
#include <stdio.h> int main() { int a = 5, b = 10, c = 2; int result1 = a + b * c; // multiplication first int result2 = (a + b) * c; // parentheses change order int result3 = b / c - a; // division then subtraction int result4 = b / (c - a); // parentheses first printf("result1 = %d\n", result1); printf("result2 = %d\n", result2); printf("result3 = %d\n", result3); printf("result4 = %d\n", result4); return 0; }
OutputSuccess
Important Notes
Remember that multiplication (*) and division (/) have higher precedence than addition (+) and subtraction (-).
Use parentheses to make your code clear and avoid mistakes.
Operator precedence applies to all operators, including logical and bitwise ones, but start with math operators first.
Summary
Operator precedence decides which part of an expression runs first.
Multiplication and division happen before addition and subtraction.
Use parentheses to control the order and make your code easier to read.