The ones who are crazy enough to think they can change the world are the ones who do.- Steve Jobs
Every Operator have their own precedence because without operator precedence a complier will conflict with data when performing mathematical calculations.
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 |
#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;
}
/ (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'.
#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;
}
/ (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.
#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;
}
We may make mistakes(spelling, program bug, typing mistake and etc.), So we have this container to collect mistakes. We highly respect your findings.
© Copyright 2019