C Operator Precedence and Associativity Table with Examples

    1 Votes


In the last two tutorials about operators in C language, we learned different types of operators, arity of operators and examples on how to use them. In this chapter we're going to learn about other important concepts about operators called operator precedence and operator associativity.

Operator Precedence

It usually means, if an expression has multiple operators in it, which operator get the precedence over others. To understand what's meant by operator precedence, let's take an arithmetic expression as an example.

Consider, int a = 2 + 3 * 4. Value of 'a' would be 14, not 20. This is because operator '*' is executed before '+', like the BODMAS rule. So we can say that operator '*' has precedence over operator '+' and the expression would be grouped as int a = (2 + (3 * 4)).

Operator Associativity

Associativity specifies how the operators are grouped for evaluation. For example, consider the expression 2 + 3 + 4. Note: In this case (+ operator), the result of different grouping doesn't matter. But that might not be the case with other operators.

can you tell how the expression will be grouped? ((2 + 3) + 4) or (2 + (3 + 4)). This is determined by the associativity of operators. From the above example we can understand that, associativity comes into picture only when expression has operators of same precedence.

Different Types of Operator Associativity

There are two types of associativity possible known as 'Right to Left' associativity and 'Left to Right' associativity. L->R (left to right) associativity means operator on the left hand side will get priority over right hand operator and R->L (right to left) associativity means right most operators will get more priority. To understand the associativity in detail, lets consider two example expressions.

  1. a = b = c : '=' has right to left associativity. So the expression will be grouped as (a = (b = c))
  2. a + b + c : '+' has left to right associativity. So the expression will be grouped as ((a + b) + c)

Also Read - Top C Interview Questions and Answers for Freshers & Advanced C Interview Questions and Answers

Precedence and Associativity Table

Table below shows the precedence and associativity of the operators available in C language. The operators in top rows have more precedence compared to the ones in bottom rows. Operators in same cell have same precedence.

Operator(s)DescriptionAssociativity
++ -- Post increment/post decrement operator Left to right associativity
++ -- Pre-increment/pre-decrement operator Right to left associativity
+ - Unary plus/minus operator Right to left associativity
! ~ Logical NOT/bitwise NOT operator Right to left associativity
* "Value at address" operator Right to left associativity
& "Address of" operator Right to left associativity
sizeof "Size of" operator Right to left associativity
* / % Multiplication/division/modulus operator Left to right associativity
+ - Addition/subtraction operator Left to right associativity
<< >> Bitwise left shift/bitwise right shift operator Left to right associativity
< <= "Less than"/"Less than or equal to" relational operator Left to right associativity
> >= "Greater than"/"Greater than or equal to" relational operator Left to right associativity
== != "Equal to"/"Not equal to" relational operator Left to right associativity
& Bitwise AND operator Left to right associativity
^ Bitwise XOR operator Left to right associativity
| Bitwise OR operator Left to right associativity
&& Logical AND operator Left to right associativity
|| Logical OR operator Left to right associativity
?: Ternary conditional operator Right to left associativity
= Assignment operator Right to left associativity
+= -= Assignment with addition/subtraction Right to left associativity
*= /= %/ Assignment with multiplication/division/modulus Right to left associativity
<<= >>= Assignment with bitwise left shift/bitwise right shift Right to left associativity
&= ^= |= Assignment with bitwise AND/bitwise XOR/bitwise OR Right to left associativity

precedence - Associativity Examples

#include <stdio.h>

main()
{
   float a = 10;
   float b = 20;
   float c = 30;
   float d = 40;
   float e;
 
   e = a + b * c / d; 
   printf("Value of a + b * c / d is    : %f\n",  e );

   e = (a + b) * c / d;
   printf("Value of (a + b) * c / d is  : %f\n" ,  e );

   e = a + (b * c) / d;
   printf("Value of a + (b * c) / d is  : %f\n",  e );

   e = a + b * (c / d);    
   printf("Value of a + b * (c / d) is  : %f\n" ,  e );
  
   return 0;
}

Output:

Value of a + b * c / d is    : 25.000000
Value of (a + b) * c / d is  : 22.500000
Value of a + (b * c) / d is  : 25.000000
Value of a + b * (c / d) is  : 25.000000

Also Read - Top Embedded C Interview Questions and Answers & C++ Interview Questions and Answers

Related Articles

C Overview and Basic Syntax
Data Types in C Language
C Qualifiers - Constant and Volatile type Qualifier
C Type Casting with examples - float to int / int to char / int to long
C Constants and Literals
C Operators - Arithmetic, Assignment, Relational and Logical
C Operators - Bitwise Operators and Conditional Operators
C Input and Output Statements
C Format Specifiers for printf and scanf
C Decision Making Control Structures - 'if', 'if - else', nested 'if', 'switch'
Loops in C - for, while and do...while Loops
C 'break', 'continue' and 'goto' Statements to Control Program Flow
C Preprocessor Directives and Macros
C Functions Tutorial with Examples
C Arrays - Dynamic, 2d and Multidimensional with Examples
C Strings - Internal Working and Functions with example
C Pointers basics explained with examples
Structures in C Programming with Example
Unions in c programming with example
C Storage Class Specifiers - auto, register, static and extern

Popular Videos

How to Overcome Shyness in Communication

How to Overcome Shyness in Communication using 4 Simple Steps.

Got a tip or Question?
Let us know