assignment-operators

“My assignment is what every writer’s assignment is: tell the truth of his own time” Leslie Fiedler

In this section we’re assuming you’re familiar with operators in general and variables.

We can use assign = operator to add or change the value in a variable. But there are also a couple of other ways that we can add a value to a variable as well.

In the list down below we gave an introduction to these operators and in the rest of this section we will try to explain and give example for each of these operators.

Operator Symbol
Assignment operator =
Addition and Assignment operator +=
Subtract and Assignment operator -=
Multiplication and Assignment operator *=
Division and Assignment operator /=

Using this operator we can assign a value to a variable.

Example:

#include <stdio.h>

int
main() {

int age = 13;

return 0;
}

In the example above, the age variable assigned the value 13 via assignment = operator.

Addition and Assignment operator `+=`:

Sometimes there’s already a value in a variable and we want to add a value to that existing value.

One way to do this is:

#include <stdio.h>

int
main() {

int age = 13;
age = age + 20;
return 0;
}

In this example we have the variable age that has the value 13. We then decided to sum the value in the variable with 20 and assign the result to the same variable.

We can achieve exactly the same result via += operator. Using this operator means add the new value to the already existing value in the variable.

Example:

#include <stdio.h>

int
main() {

int age = 13;
age += 20;
return 0;
} The use of ` age += 20;` is exactly the same as if we used ` age = age + 20;`

Subtraction and Assignment operator `-=`:

Sometimes there’s already a value in a variable and we want to subtract a value from that existing value.

One way to do this is:

#include <stdio.h>

int
main() {

int age = 13;
age = age - 10;
return 0;
}

In this example we have the variable age that has the value 13. We then decided to subtract the value in the variable from 10 and assign the result to the same variable.

We can achieve exactly the same result via -= operator. Using this operator means subtract the already existing value in the variable from the new value and assign the result to the variable itself.

Example:

#include <stdio.h>

int
main() {

int age = 13;
age -= 10;
return 0;
} The use of ` age -= 10;` is exactly the same as if we used ` age = age - 10;`

Multiplication and Assignment operator `*=`:

Sometimes there’s already a value in a variable and we want to multiply a value to that existing value.

One way to do this is:

#include <stdio.h>

int
main() {

int age = 13;
age = age * 20;
return 0;
}

In this example we have the variable age that has the value 13. We then decided to multiply the value in the variable with 20 and assign the result to the same variable.

We can achieve exactly the same result via *= operator. Using this operator means multiply the new value to the already existing value in the variable and assign the result to the variable itself.

Example:

#include <stdio.h>

int
main() {

int age = 13;
age *= 20;
return 0;
} The use of ` age *= 20;` is exactly the same as if we used ` age = age * 20;`

Division and Assignment operator `/=`:

Sometimes there’s already a value in a variable and we want to divide that value by another value and assign the result to the same variable.

One way to do this is:

#include <stdio.h>

int
main() {

double height = 200;
height = height / 20;
return 0;
}

In this example we have the variable height that has the value 200. We then decided to divide the value in the variable by 20 and assign the result to the same variable.

We can achieve the same result via /= operator. Using this operator means divide the value in the variable by the new value and assign the result to the variable itself.

Example:

#include <stdio.h>

int
main() {

double height = 13;
height /= 20;
return 0;
} The use of ` height /= 20;` is exactly the same as if we used ` height = height / 20;`