- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Operator Precedence
![]() Share with a Friend |
C Programming - C Operator Precedence
C Operator Precedence
Operator precedence in C determines the order in which operators are evaluated in expressions. Operators with higher precedence are evaluated before those with lower precedence. In cases where operators have the same precedence, associativity decides the evaluation order (whether they are evaluated from left to right or right to left).
Operator Precedence in C
The following table summarizes the precedence of operators in C, listed from the highest to the lowest precedence:
| Precedence Level | Operator(s) | Description |
|---|---|---|
|
1 |
() |
Parentheses (used for grouping expressions) |
|
2 |
[] -> . |
Array subscripting, member selection, pointer dereference |
|
3 |
++ -- (prefix) |
Increment and Decrement (prefix form) |
|
4 |
! ~ + - (unary) |
Logical NOT, Bitwise NOT, Unary plus, Unary minus |
|
5 |
* / % |
Multiplication, Division, Modulus |
|
6 |
+ - |
Addition, Subtraction |
|
7 |
<< >> |
Bitwise left shift, Bitwise right shift |
|
8 |
< <= > >= |
Relational operators |
|
9 |
== != |
Equality and Inequality operators |
|
10 |
& |
Bitwise AND |
|
11 |
^ |
Bitwise XOR |
|
12 |
` |
` |
|
13 |
&& |
Logical AND |
|
14 |
` |
|
|
15 |
? : |
Ternary conditional operator |
|
16 |
= += -= *= /= %= &= ^= ` |
= <<= >>=` |
|
17 |
, |
Comma operator |
Associativity of Operators
- Left-to-right associativity: Most operators in C (such as arithmetic, relational, logical, and bitwise operators) follow left-to-right evaluation.
- Example: a + b - c is evaluated as ((a + b) - c).
- Right-to-left associativity: Assignment operators and the ternary conditional operator (? :) follow right-to-left evaluation.
- Example: a = b = c is evaluated as a = (b = c).
Operator Precedence Rules
- Parentheses ():
- Anything inside parentheses is evaluated first, overriding all operator precedences.
- Example: (a + b) * c evaluates a + b first, then multiplies the result by c.
- Unary Operators (++, --, +, -, !, ~):
- These operators have higher precedence than most other operators, so they are evaluated first.
- Example: ++a + b increments a before adding b.
- Multiplication, Division, Modulus (*, /, %):
- These operators have higher precedence than addition and subtraction, so they are evaluated first when mixed.
- Example: a + b * c evaluates as a + (b * c).
- Relational Operators (<, <=, >, >=):
- These operators have lower precedence than arithmetic and bitwise operators but are evaluated before logical operators.
- Example: a + b > c evaluates a + b first, then compares the result to c.
- Logical Operators (&&, ||):
- These operators have lower precedence than relational and arithmetic operators but higher precedence than assignment operators.
- Example: a > b && c > d evaluates both conditions before the logical AND operation.
- Assignment Operators (=, +=, -=):
- Assignment operators have the lowest precedence (except for the comma operator).
- Example: a = b + c first evaluates b + c, then assigns the result to a.
Parentheses to Control Precedence
You can always use parentheses () to control the order of evaluation explicitly in expressions, even when the operator precedence gives a different order.
Example Without Parentheses:
C
int result = 5 + 2 * 3;
Here, * has higher precedence than +, so the result is:
C
5 + (2 * 3) = 5 + 6 = 11
Example With Parentheses:
C
int result = (5 + 2) * 3;
Here, parentheses override the precedence, so the result is:
C
(5 + 2) * 3 = 7 * 3 = 21
Operator Precedence in Action
C
#include <stdio.h>
int main() {
int a = 5, b = 10, c = 20, result;
// Applying precedence rules
result = a + b * c; // result = 5 + (10 * 20) = 5 + 200 = 205
printf("Result of a + b * c: %d\n", result);
result = (a + b) * c; // result = (5 + 10) * 20 = 15 * 20 = 300
printf("Result of (a + b) * c: %d\n", result);
result = a + b > c; // result = (5 + 10) > 20 = 15 > 20 = 0 (false)
printf("Result of a + b > c: %d\n", result);
return 0;
}
Output:
Result of a + b * c: 205
Result of (a + b) * c: 300
Result of a + b > c: 0
Summary
- The precedence of operators determines the order in which operations are performed in C expressions.
- Parentheses can be used to override default precedence to ensure the desired order of evaluation.
- Operators with higher precedence are evaluated first, and associativity defines how operators with the same precedence are evaluated.
