
- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- 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 - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs 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
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- Structures and Unions in C
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- 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 - Function Pointers
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Pointer to an Array
- C - Pointers vs. Multi-dimensional Arrays
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Initialization of Pointer Arrays
- Storage Classes and Qualifiers
- C - Storage Classes
- Memory Management in C
- C - Memory Management
- C - Memory Address
- Preprocessors in C
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- File Handling in C
- C - File I/O (File Handling)
- C - Input & Output
- Constants and Literals in C
- C - Macros
- C - Header Files
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
- C Online Compiler
Operator Precedence in C
A single expression in C may have multiple operators of different types. The C compiler evaluates its value based on the operator precedence and associativity of operators.
The precedence of operators determines the order in which they are evaluated in an expression. Operators with higher precedence are evaluated first.
For example, take a look at this expression −
x = 7 + 3 * 2;
Here, the multiplication operator "*" has a higher precedence than the addition operator "+". So, the multiplication 3*2 is performed first and then adds into 7, resulting in "x = 13".
The following table lists the order of precedence of operators in C. Here, operators with the highest precedence appear at the top of the table, and those with the lowest appear at the bottom.
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 |
Within an expression, higher precedence operators will be evaluated first.
Operator Associativity
In C, the associativity of operators refers to the direction (left to right or right to left) an expression is evaluated within a program. Operator associativity is used when two operators of the same precedence appear in an expression.
In the following example −
15 / 5 * 2
Both the "/" (division) and "*" (multiplication) operators have the same precedence, so the order of evaluation will be decided by associativity.
As per the above table, the associativity of the multiplicative operators is from Left to Right. So, the expression is evaluated as −
(15 / 5) * 2
It evaluates to −
3 * 2 = 6
Example 1
In the following code, the multiplication and division operators have higher precedence than the addition operator.
The left−to−right associativity of multiplicative operator results in multiplication of "b" and "c" divided by "e". The result then adds up to the value of "a".
#include <stdio.h> int main(){ int a = 20; int b = 10; int c = 15; int d = 5; int e; e = a + b * c / d; printf("e : %d\n" , e ); return 0; }
Output
When you run this code, it will produce the following output −
e: 50
Example 2
We can use parenthesis to change the order of evaluation. Parenthesis () got the highest priority among all the C operators.
#include <stdio.h> int main(){ int a = 20; int b = 10; int c = 15; int d = 5; int e; e = (a + b) * c / d; printf("e: %d\n", e); return 0; }
Output
Run the code and check its output −
e: 90
In this expression, the addition of a and b in parenthesis is first. The result is multiplied by c and then the division by d takes place.
Example 3
In the expression that calculates e, we have placed a+b in one parenthesis, and c/d in another, multiplying the result of the two.
#include <stdio.h> int main(){ int a = 20; int b = 10; int c = 15; int d = 5; int e; e = (a + b) * (c / d); printf("e: %d\n", e ); return 0; }
Output
On running this code, you will get the following output −
e: 90
Precedence of Post / Prefix Increment / Decrement Operators
The "++" and "− −" operators act as increment and decrement operators, respectively. They are unary in nature and can be used as a prefix or postfix to a variable.
When used as a standalone, using these operators in a prefix or post−fix manner has the same effect. In other words, "a++" has the same effect as "++a". However, when these "++" or "− −" operators appear along with other operators in an expression, they behave differently.
Postfix increment and decrement operators have higher precedence than prefix increment and decrement operators.
Example
The following example shows how you can use the increment and decrement operators in a C program −
#include <stdio.h> int main(){ int x = 5, y = 5, z; printf("x: %d \n", x); z = x++; printf("Postfix increment: x: %d z: %d\n", x, z); z = ++y; printf("Prefix increment. y: %d z: %d\n", y ,z); return 0; }
Output
Run the code and check its output −
x: 5 Postfix increment: x: 6 z: 5 Prefix increment. y: 6 z: 6
Logical operators have left−to−right associativity. However, the compiler evaluates the least number of operands needed to determine the result of the expression. As a result, some operands of the expression may not be evaluated.
For example, take a look at the following expression −
x > 50 && y > 50
Here the second operand "y > 50" is evaluated only if the first expression evaluates to True.