Precedence of operator:
It is the priority of an operator.For example, suppose you are said to solve the following simple math:
12 + 10 /2 + 3*2 – 5
You will do this as following steps:
step1: Divide 10 by 2. Now 12 + 10 /2 + 3*2 – 5 is equal to 12 + 5 + 3*2 – 5
step2: Multiply 3 by 2.Now 12 + 5 + 3*2 – 5 is equal to 12 + 5 + 6 – 5
step3: Add 12,5 and 6.Now 12 + 5 + 6 – 5 is equal to 23-5
step4: Subtract 5 from 23. Now 23-5 is equal to 18
And finally your result is 18.
Here you have given different priorities on different operators. Most priority was given to ‘/’ operator then * then + then –
In the same manner, compiler executes such math problem giving this sequence of priority of operator. The following code is the evidence of the above discussion:
#include<stdio.h> int main() { int a; a=12 + 10/2 + 3*2 - 5; printf("a=%d",a); return 0; }
OUTPUT
a=18
Associativity of operator:
It defines Whether the evaluation of an expression will start from left to right or right to left.
Next Previous