We will start with a simple example of c – code :
int i = 10, j = 20 , k;
printf(“i=%d j=%d k=%d\n”,i,j,k);
k = i + j;
printf(“i = %d j = %d k = %d\n”, i , j , k);
What is the answer to above example ?
If you know the answer please do comment in comment section belowno.
Every Operator in C – Language has a Priority.
” man operator ” to see the priority of operator. (press ” q ” to exit from that page)
+= and *= is a compound assignement operators. (ex. a+=3 or a = a+3 both do same work)
If one or more operator have same priority than we have to think about ASSOCIATIVITY. (Left to right or right to left.)
ex. k = i/j * k; <———-Here it will start execution from left to right that means first i/j will execute and later that answer of (i/j) will multiply with k.
Note that all the arithmetic operators will not modify the operands.
If you want to change the priority then make a grouping.
i.e. k = i+ j * k ; (here + has a lower priority than the * )
k = (i + j) * k ; (So, here after making a grouping, the grouping part will execute first and the result will multiply with k)
We will see that using one simple example:
j = i + j – (i = j); <——–Group has highest priority.
= i + j – expression; <——–First of all, it will assign a grouping.
= 10 + 20 – Expression;
= 30 – Expression;
= 30 – 20;
j =10; and i = 20;