The ones who are crazy enough to think they can change the world are the ones who do.
- Steve Jobs

Operator Precedence In C

Why Operator Precedence?

Every Operator have their own precedence because without operator precedence a complier will conflict with data when performing mathematical calculations.

Example

Consider the following expression 6 - 4 + 8 without operator precedence compiler is helpless to choose which operator needs to execute first. Thus Operator Precedence helps compiler out there.

The following table lists all C operators and their precedence from higher priority to lower priority

Operator Operation Clubbing Priority
( ) Function call Left to Right 1st
[ ] Array " "
Structure Operator " "
. Structure Operator " "
+ Unary plus Right to Left 2nd
- Unary minus " "
++ Increment " "
-- Decrement " "
! Not Operator " "
~ One's Complement " "
* Pointer Operation " "
& Address Operator " "
sizeof Size of an data type " "
(typecast) Type Cast " "
* Multipication Left to Right 3rd
/ Division " "
% Modular Division " "
+ Addition Left to Right 4th
- Subtraction " "
<< Left shift Left to Right 5th
>> Right shift " "
< Less than Left to Right 6th
<= Less than or equal to " "
> Greater than " "
>= Greater than or Equalto " "
= Equality Left to Right 7th
!= InEquality " "
& Bitwise AND Left to Right 8th
^ Bitwise XOR Left to Right 9th
| Bitwise OR Left to Right 10th
&& Logical AND Left to Right 11th
|| Logical OR Left to Right 12th
?: Conditional Operator Right to Left 13th
^=, !=, <<=, > >= Assignment Operator Right to Left 14th
, comma operator Right to Left 15th

Operator precedence program(Easy)

operatorprecedence1.c
#include <stdio.h> //header file section
int main()
{
int a = 2, b = 6, c = 12, d;
d = a + c / b;
printf("The value of d = %d ", d);
return 0;
}
  • The value of d = 4

Note:

/ (Division operator) executed first. Now the expression will be d = a + 2. Finally + (Addition operator) executed and the resultant value 4 is stored in a variable 'd'.

Operator precedence in C(Medium)

operatorprecedence2.c
#include <stdio.h> //header file section
int main()
{
int a = 2, b = 6, c = 12, d;
d = a * b + c / b;
printf("The value of d = %d ", d);
return 0;
}
  • The value of d = 14

Note:

/ (Division operator) executed first. Now the expression is d = a * b + 2. Secondly, * (Multiplication operator) will be executed, now the expresssion becomes d = 12 + 2 Finally + (Addition operator) will be executed and store the value 14 in a variable d.

Operator Precedence in C(Hard)

operatorprecedence3.c
#include <stdio.h> //header file section
int main()
{
int a = 10, b = 10, c = 1, d = 5, r;
r = ++a + (++b) + b-- * (++c) + --d;
printf("The value of r = %d ", r);
return 0;
}
  • The value of r = 48

Note:

  • Step 1: r = 11 + 11 + b-- * 2 + --d
  • Step 2: r = 11 + 11 + 11 * 2 + 4
  • Step 3: r = 11 + 11 + 22 + 4
  • Step 4: r = 48.
  • Report Us

    We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.

    Report