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

What is object oriented design patterns

A design pattern is a reusable solution to a commonly occurring problem in software design. They…

4 months ago

Factory Method Design Pattern in OODP

Factory Method is a creational design pattern that deals with the object creation. It separates…

4 months ago

Find Intersection of Two Singly Linked Lists

You are given two singly linked lists that intersect at some node. Your task is…

10 months ago

Minimum Cost to Paint Houses with K Colors

A builder plans to construct N houses in a row, where each house can be…

10 months ago

Longest Absolute Path in File System Representation

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

10 months ago

Efficient Order Log Storage

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

11 months ago