- C Programming Tutorial
- C - Home
- Basics of C
- C - Overview
- C - Features
- C - History
- C - Environment Setup
- 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 - 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
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.