For working professionals
For fresh graduates
More
5. Array in C
13. Boolean in C
18. Operators in C
33. Comments in C
38. Constants in C
41. Data Types in C
49. Double In C
58. For Loop in C
60. Functions in C
70. Identifiers in C
81. Linked list in C
83. Macros in C
86. Nested Loop in C
97. Pseudo-Code In C
100. Recursion in C
103. Square Root in C
104. Stack in C
106. Static function in C
107. Stdio.h in C
108. Storage Classes in C
109. strcat() in C
110. Strcmp in C
111. Strcpy in C
114. String Length in C
115. String Pointer in C
116. strlen() in C
117. Structures in C
119. Switch Case in C
120. C Ternary Operator
121. Tokens in C
125. Type Casting in C
126. Types of Error in C
127. Unary Operator in C
128. Use of C Language
If you've ever written a mathematical expression in C and scratched your head wondering why the result wasn’t what you expected, you’re not alone. This confusion often stems from not fully understanding Operator Precedence and Associativity in C. These two concepts dictate how expressions are evaluated and in what order operations are performed. That’s why you’ll find this concept in all top-rated software development courses.
In this blog, we’ll break down Operator precedence in C and Operator Associativity in C in a clear, conversational way with simple code examples. By the end, you'll not only understand how the C compiler evaluates complex expressions, but also how to use Operator Precedence and Associativity in C to your advantage.
Before you start with operator precedence in C, you should explore our following articles to strengthen your foundational knowledge:
In C programming, expressions often involve multiple operators. But how does the compiler know which operation to perform first? That’s where operator precedence in C comes in. It defines the priority level of operators—deciding which operator is evaluated before others in a complex expression.
Think of it like solving a math problem: you multiply before you add. In the same way, C follows a hierarchy. For instance, the multiplication operator `*` has higher precedence than the addition operator `+`. So, in an expression like `3 + 4 * 5`, the multiplication happens first, not the addition.
Let’s break it down with a code example.
#include <stdio.h>
int main() {
int result;
// Example of Operator Precedence: * has higher precedence than +
result = 3 + 4 * 5;
printf("Result: %d\n", result);
return 0;
}
Output:
Result: 23
Explanation:
This is a simple but powerful example of how operator precedence in C directly affects the result of your expressions.
Unlock a high-paying opportunity at a Fortune 500 with following full-stack development courses:
When multiple operators of the same precedence appear in an expression, how does the compiler determine the order of evaluation? That’s exactly what Operator Associativity in C helps clarify. It defines the direction in which operators of the same precedence level are evaluated—either left-to-right or right-to-left.
For instance, in the expression `a - b + c`, both `-` and `+` have the same precedence. Because these operators are left-associative, the expression is evaluated as `(a - b) + c`. If they were right-associative, it would be evaluated as `a - (b + c)` instead.
Let’s make this clearer with a code example.
#include <stdio.h>
int main() {
int a = 10, b = 5, c = 2, result;
// + and - have the same precedence and left-to-right associativity
result = a - b + c;
printf("Result: %d\n", result);
return 0;
}
Output:
Result: 7
Explanation:
Understanding operator Associativity in C helps ensure expressions are evaluated the way you intend, especially when chaining multiple operators of equal precedence.
To master Operator Precedence and Associativity in C, it's crucial to have a reliable reference. Below is a structured table that shows the precedence levels of operators along with their associativity. Operators higher in the table have higher precedence.
Precedence Level | Operators | Description | Associativity |
1 (Highest) | (), [], ->, . | Function call, array subscripting, structure/union member access | Left to Right |
2 | ++, --, +, -, !, ~, *, &, sizeof, typecast | Unary operators, type cast, dereference, address-of, sizeof | Right to Left |
3 | *, /, % | Multiplication, division, modulo | Left to Right |
4 | +, - | Addition, subtraction | Left to Right |
5 | <<, >> | Bitwise shift left, shift right | Left to Right |
6 | <, <=, >, >= | Relational operators | Left to Right |
7 | ==, != | Equality operators | Left to Right |
8 | & | Bitwise AND | Left to Right |
9 | ^ | Bitwise XOR | Left to Right |
10 | ` | ` | Bitwise OR |
11 | && | Logical AND | Left to Right |
12 | ` | ` | |
13 | ?: | Ternary conditional | Right to Left |
14 | =, +=, -=, *=, /=, %=, <<=, >>=, &=, ^=, ` | =` | Assignment operators |
15 (Lowest) | , | Comma operator | Left to Right |
This table is an essential part of understanding operator precedence in C and operator Associativity in C. Keep it handy whenever you’re writing or debugging complex expressions.
Let’s explore the top 5 example of operator Precedence in C
This example shows how multiplication takes precedence over addition in a simple arithmetic expression. To develop such programs efficiently, you should learn about the structure of a C program.
#include <stdio.h>
int main() {
int result;
// * has higher precedence than +
result = 2 + 3 * 4;
printf("Result: %d\n", result);
return 0;
}
Output:
Result: 14
Explanation:
Parentheses allow you to override default precedence and control the order of evaluation.
#include <stdio.h>
int main() {
int result;
// Parentheses force addition before multiplication
result = (2 + 3) * 4;
printf("Result: %d\n", result);
return 0;
}
Output:
Result: 20
Explanation:
This example shows how arithmetic operators take precedence over relational operators like `<`.
#include <stdio.h>
int main() {
int result;
// + is evaluated before <
result = 5 + 2 < 10;
printf("Result: %d\n", result);
return 0;
}
Output:
Result: 1
Explanation:
Here, we see how assignment has lower precedence than arithmetic operations.
#include <stdio.h>
int main() {
int a = 5, b = 2, result;
// * is evaluated before =
result = a * b + 1;
printf("Result: %d\n", result);
return 0;
}
Output:
Result: 11
Explanation:
This example demonstrates how C handles a combination of arithmetic, relational, and logical operators based on precedence.
#include <stdio.h>
int main() {
int a = 4, b = 5, c = 6;
int result;
// Arithmetic before relational, relational before logical
result = a + b > c && c > b;
printf("Result: %d\n", result);
return 0;
}
Output:
Result: 1
Explanation:
1. Arithmetic: `a + b = 4 + 5 = 9`
2. Relational: `9 > 6` → true (`1`), and `6 > 5` → true (`1`)
3. Logical AND (`&&`): `1 && 1 = 1` (both conditions true)
These examples highlight how operator precedence in Censures consistent and predictable evaluation. Mastering this can help you avoid parentheses overuse, debug faster, and write cleaner code. By combining multiple operators, you can also create a calculator. For its in-depth insights, explore our article on creating calculator program in C.
Now that we have covered Operator Precedence in C, let’s explore Operator Associativity in C. Associativity comes into play when operators with the same precedence level are involved. It dictates whether operators are evaluated from left to right or right to left.
Let’s dive into some examples to understand this concept more clearly.
Arithmetic operators like `+`, `-`, `*`, and `/` follow left-to-right associativity in C. This means that when you have multiple operators of the same precedence, the expression is evaluated from left to right.
#include <stdio.h>
int main() {
int result;
// + and - have left-to-right associativity
result = 10 - 5 + 3;
printf("Result: %d\n", result);
return 0;
}
Output:
Result: 8
Explanation:
Assignment operators (`=`, `+=`, `-=`, etc.) have right-to-left associativity. This means that in an expression involving multiple assignment operators, the rightmost assignment is performed first.
#include <stdio.h>
int main() {
int a, b, c;
// Assignment operators have right-to-left associativity
a = b = c = 10;
printf("a = %d, b = %d, c = %d\n", a, b, c);
return 0;
}
Output:
a = 10, b = 10, c = 10
Explanation:
Logical operators like `&&` (AND) and `||` (OR) also follow left-to-right associativity in C.
#include <stdio.h>
int main() {
int result;
// && has left-to-right associativity
result = 1 && 0 && 1;
printf("Result: %d\n", result);
return 0;
}
Output:
Result: 0
Explanation:
Comparison operators like `==`, `!=`, `<`, `<=`, `>`, and `>=` also follow left-to-right associativity.
#include <stdio.h>
int main() {
int result;
// Comparison operators have left-to-right associativity
result = 5 < 10 == 5;
printf("Result: %d\n", result);
return 0;
}
Output:
Result: 1
Explanation:
The ternary conditional operator (`?:`) in C has right-to-left associativity. This means when you have nested ternary operators, the evaluation starts from the rightmost operator.
#include <stdio.h>
int main() {
int result;
// Ternary operator has right-to-left associativity
result = (5 < 10) ? (10 < 20 ? 1 : 0) : 2;
printf("Result: %d\n", result);
return 0;
}
Output:
Result: 1
Explanation:
These examples showcase how operator associativity in C dictates the order in which operators with the same precedence are evaluated. Understanding this concept can help you write clearer and more accurate expressions, especially when dealing with complex statements that involve multiple operators.
Also learn about compiling C program in Linux, and about C compiler in Mac to develop programs on any operating system.
Operator Precedence in C
Operator Precedence dictates the order in which operators are evaluated. Operators with higher precedence are evaluated first. For example, multiplication (`*`) is performed before addition (`+`). Parentheses can be used to explicitly control the evaluation order and override the default precedence.
Operator Associativity in C
Operator Associativity determines the direction of evaluation for operators of the same precedence. Most operators follow left-to-right associativity, meaning they are evaluated from left to right. However, assignment operators (`=`, `+=`, etc.) follow right-to-left associativity, meaning they are evaluated from right to left.
Use Parentheses for Clarity
Although Operator Precedence and Associativity dictate evaluation order, using parentheses ensures clarity and eliminates ambiguity in complex expressions. Parentheses help specify the evaluation order explicitly, making the code more readable and predictable.
Caution with Assignment Operators
Assignment operators have right-to-left associativity, so expressions like `a = b = c = 10` are evaluated from right to left. Understanding this behavior is essential to avoid unexpected results when using multiple assignments.
Know the Precedence of Operators
Knowing the precedence of operators ensures correct evaluation. For example, the postfix increment (`++`) and dereference (`*`) operators have the highest precedence and are evaluated before other operators. Understanding this helps in writing more predictable and error-free code.
Understanding Operator Precedence and Associativity in C is vital for writing efficient and bug-free code. These two concepts determine how expressions are evaluated and can impact the logic of your program. By knowing the precedence rules, you can predict how operators interact in complex expressions, while understanding associativity ensures the correct order when operators have the same precedence.
Using parentheses is a good practice to make the evaluation order explicit, reducing ambiguity and improving the readability of your code. Special attention should be given to right-to-left associativity in assignment and ternary operators, as these can easily lead to unexpected results if not handled correctly.
Mastering Operator Precedence in C and Operator Associativity in C allows you to write cleaner, more reliable programs and avoid logical errors, helping you become a more proficient C programmer.
Operator precedence in C determines the order in which operators are applied in an expression. For example, multiplication and division have higher precedence than addition and subtraction, so they are evaluated first. Understanding precedence helps avoid errors in expressions where multiple operators are involved. However, precedence alone isn't always enough; associativity also plays a crucial role.
Operator associativity in C governs how operators of the same precedence are evaluated. Most operators, such as arithmetic and logical operators, follow left-to-right associativity, meaning they are evaluated in the order they appear from left to right. However, some operators like assignment (`=`) and the ternary conditional (`?:`) follow right-to-left associativity, meaning they are evaluated from right to left.
Parentheses in C are used to override the default precedence of operators. By explicitly defining the order of operations within parentheses, you ensure that complex expressions are evaluated in the desired order. This reduces the risk of errors, especially in cases where operator precedence and associativity could lead to unexpected results without clear parentheses.
If operator precedence is ignored, an expression may produce unintended results. For example, "2 + 3 * 4" is evaluated as "2 + (3 * 4)" because multiplication has higher precedence than addition. Not considering precedence may lead to logic errors, causing the program to behave differently than expected and possibly leading to incorrect calculations or outcomes.
In C, assignment operators like `=`, `+=`, and `-=` are used to assign values to variables. These operators have right-to-left associativity. This means that in an expression like "a = b = c = 10", the assignment starts with `c = 10`, then `b = c`, and finally `a = b`. Understanding this behavior is essential for avoiding confusion when multiple assignments are involved in a single line of code.
Yes, operators with different precedence can be mixed in the same expression. However, the result depends on the precedence rules. Operators with higher precedence are evaluated first. For example, in the expression "2 + 3 * 4", multiplication is performed before addition because `*` has higher precedence than `+`. To ensure the correct evaluation order, parentheses are often used.
The ternary operator `?:` is a shorthand for an if-else statement and follows right-to-left associativity. It evaluates the condition first, then returns one of two values based on whether the condition is true or false. For example, `x = (a > b) ? a : b` assigns the greater of `a` and `b` to `x`. It's an efficient way to perform conditional assignments but requires understanding associativity.
Yes, associativity can affect the outcome of an expression, especially when multiple operators of the same precedence are used. For example, in an expression like "a = b = c = 10", the assignment happens from right to left because the assignment operator has right-to-left associativity. Without understanding this behavior, it might seem like the assignments should occur from left to right, leading to potential errors.
Logical operators like AND (`&&`) and OR (`||`) follow **left-to-right associativity** and have lower precedence than arithmetic and relational operators. This means that in an expression with mixed operators, such as "x > 5 && y < 10", the relational operators are evaluated first, followed by the logical AND. The evaluation follows the order of precedence and associativity, ensuring accurate results.
The comma operator `,` allows multiple expressions to be evaluated in sequence within a single statement. It has very low precedence, meaning it is evaluated last in an expression. The value of the entire expression is the value of the last expression evaluated. For example, in `a = (x++, y++)`, the comma operator ensures that both increments happen before the assignment, but only `y++` is assigned to `a`.
In complex expressions with multiple operators, precedence plays a crucial role in determining the order of evaluation. Without proper understanding, it’s easy to make logical mistakes. For example, in an expression like `x + y * z`, multiplication is performed first because it has higher precedence than addition. To avoid such issues, developers can use parentheses to explicitly define the order of operations, preventing unintended results.
Take a Free C Programming Quiz
Answer quick questions and assess your C programming knowledge
Author
Start Learning For Free
Explore Our Free Software Tutorials and Elevate your Career.
Talk to our experts. We are available 7 days a week, 9 AM to 12 AM (midnight)
Indian Nationals
1800 210 2020
Foreign Nationals
+918068792934
1.The above statistics depend on various factors and individual results may vary. Past performance is no guarantee of future results.
2.The student assumes full responsibility for all expenses associated with visas, travel, & related costs. upGrad does not provide any a.