Categories: C

Precedence of Operators in C

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.

Priority of Operators:

Let us take a look at the precedence of commonly used operators :

here the priority goes from high(* / %) to low(=):

OperatorDescription
* / %multiplication, division, modular division
+ –addition, subtraction
=assignment
Associativity of Operators:

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:

  • Left to right associativity means that the left operand must be unambiguous i.e., it must not be involved in the evaluation of any other sub-expression.
  • Likewise, in case of right to left associativity, the right operand must be ambiguous.
CategoryOperatorAssociativity
Postfix() [] -> . ++ – –Left to right
Unary+ – ! ~ ++ – – (type)* & sizeofRight 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
Associativity with high to low priority

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.

Follow Me

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

Share
Published by
Rabecca Fatima

Recent Posts

Longest Absolute Path in File System Representation

Find the length of the longest absolute path to a file within the abstracted file…

4 days ago

Efficient Order Log Storage

You manage an e-commerce website and need to keep track of the last N order…

2 weeks ago

Select a Random Element from a Stream

You are given a stream of elements that is too large to fit into memory.…

3 weeks ago

Estimate π Using Monte Carlo Method

The formula for the area of a circle is given by πr². Use the Monte…

1 month ago

Longest Substring with K Distinct Characters

Given an integer k and a string s, write a function to determine the length…

1 month ago

Staircase Climbing Ways

There is a staircase with N steps, and you can ascend either 1 step or…

1 month ago