While executing an arithmetic operation the precedence of the operators in C as well as other programming language matters a lot because precedence determines the grouping of terms in an expression and decides how an expression is evaluated. For instance, the expression might be having various arithmetic operators like ‘+’, ‘-‘, or ‘*’; then how to know which part of the expression gets executed first.
Let us take a look at the precedence of commonly used operators :
here the priority goes from high(* / %) to low(=):
Operator | Description |
* / % | multiplication, division, modular division |
+ – | addition, subtraction |
= | assignment |
When an expression contains two operators of equal priority the tie between them is settled using the associativity of the operators. Associativity can be of two types – left to right or right to left:
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 |
Consider the expression-
a = 3 / 1 * 5 ;
output: 15
Here there is a tie between operators of same priority, that is between / and *. This tie is settled using the associativity of / and * since both / and * have L to R associativity and only / has unambiguous left operand ,therefore / is performed earlier.
note: also read about Variables & Constants in C & Operators in C.
If you like my post please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…