# 6.2 – Assigning operators and arithmetic assigning operators

by on September 26, 2017

### Assigning operator in C language:

• It is a binary operator used in between any two operands.
• Its associativity is right to left.
• It is an important operator used to assign a value of a constant, variable or an expression to a variable.
• The part to the right side of assigning operator is called r-value. This may be a variable, constant or an expression
• The part to the left side of assigning operator is called l-value. This must be a variable. ```x=45;
```

Here, a constant is being assigned to the l-value (variable)

```x=40;
y=x+50;
```

Here, an expression is being assigned to l-value (variable)

```x=y=z=10;
```

Here, expression has assigning operator for three times. As the associativity of assigning operator is right to left 10 assigns to z first, z assigns to y second and y assigns to x at the final

```34=g;
```

Here it is illegal to write a constant as l-value.

```x=30;
x+3=y;
```

Here, it is illegal to write an expression as l-value

Quiz:
1) What would be the output of following program?

```#include<stdio.h>
int main()
{
int x,y,z;
x=y=z=10;
printf("%d\t%d\t%d",x,y,z);
return 0;
}
```

2) What would be the output of following program?

```#include<stdio.h>
int main()
{
int x=10,y,z;
z=y=x+40;
printf("%d\t%d\t%d",x,y,z);
return 0;
}
```

3) What would be the output of following program?

```#include<stdio.h>
int main()
{
int x=10,y,z;
y=x+20;
x+y=z;
printf("%d\t%d\t%d",x,y,z);
return 0;
}
```

4) What would be the output of following program?

```#include<stdio.h>
int main()
{
int x=10,y,z;
printf("%d\t%d\t%d",x,y=x+z,z=x);
return 0;
}
```

5) What would be the output of following program?

```#include<stdio.h>
int main()
{
int x=10,y;
x=40+y=x+10;
return 0;
}
```

6) What would be the output of following program?

```#include<stdio.h>
int main()
{
int x=y=z=10;
printf("%d\t%d\t%d",x,y,z);
return 0;
}
```
1) 10     10    10

2) 10     50    50

3) Error: l- value required (l-value can’t be an expression)

4) 10   20     10   // order of execution is z=x   y=x+z  and  x

5) Error: l-value required (l-value to x+10 is 40+y )

6) Error   (Only x is declared, y and z are not declared)

### Arithmetic assigning operators:

Most of the times we need to add, subtract, multiply or divide a variable by a value and needs to assign the result to the same variable. Arithmetic assigning operators made this job so simple. Say for example, if we want to add 10 to x then we can write as x=x+10, the same thing can be written as x+=10 using += operator.
All the arithmetic assigning operators have the same precedence and associativity from right to left

 Operator Example Meaning += x+=y x=x+y -= x-=y x=x-y *= x*=y x=x*y /= x/=y x=x/y %= x%=y x=x%y

```#include<stdio.h>
int main()
{
int x=10;
x+=80;             /* equals to x=x+80 */
printf("%d",x);
x-=80;               /* equals to x=x-80  */
printf("\t%d",x);
x*=20;              /* equals to x=x*20  */
printf("\t%d",x);
x/=20;               /* equals to x=x/20  */
printf("\t%d",x);
x%=4;               /* equals to x=x%4  */
printf("\t%d",x);
return 0;
}
```

Output:
90      10      200     10      2

Quiz:
1) What would be the output of following program?

```#include<stdio.h>
int main()
{
int x=10,y=30,z;
z=y-=x+=5;
printf("%d\t%d\t%d",x,y,z);
return 0;
}
```

2) What would be the output of following program?

```#include<stdio.h>
int main()
{
int x=10,y=30,z=5,p;
p=z*=y+=x/=2;
printf("%d\t%d\t%d",x,y,z);
return 0;
}
```

3) What would be the output of following program?

```#include<stdio.h>
int main()
{
int x=10,y=30,z=6;
z*=y-=x+=10;
printf("%d\t%d\t%d",x,y,z);
return 0;
}
```

4) What would be the output of following program?

```#include<stdio.h>
int main()
{
int x=10,y=20,z;
z=x*=2+=y;
printf("%d\t%d\t%d\t%d",x,y,z);
return 0;
}
```

5) What would be the output of following program?

```#include<stdio.h>
int main()
{
int x=-2,y=5,z=-8,p;
p=z-=y*=x+=-9;
printf("%d\t%d\t%d\t%d",x,y,z,p);
return 0;
}
```