Arithmetic Operator

We will start with a simple example of c – code :

#include<stdio.h>

#include<conio.h>

main()

{

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;

 

 

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: