C Assignment Operators Tutorial

In this section, we will learn what the assignment operators are and how to use them in C.

Note: we’re assuming you’re already familiar with the operators and variables in C Programming.

What Is Assignment Operator in C?

The assignment operators are used to add a value to a variable or replace the currently added value of a variable with another value.

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

List of assignment operators in C

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

C Assignment `=` Operator:

Using this operator, we can assign a value to a variable. Note that this operator will replace whatever is currently in the target variable with a new value. So basically the old value will be gone after the operation.

Assignment `=` Operator Syntax:

lValue = rValue;

Assignment `=` Operator Example:

#include <stdio.h>

int main() {

    int age = 13;

    return 0;
}

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

C 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 a variable.

Addition and Assignment `+=` Operator Syntax:

lValue += rValue;

Addition and Assignment `+=` Operator 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;`

C Subtract 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 then assign the result to the variable itself again.

Subtract and Assignment `-=` Operator Syntax:

lValue -= rValue;

Subtract and Assignment `-=` Operator 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;`

C 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 by 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.

Multiplication and Assignment `*=` Operator Syntax:

lValue *= rValue;

Multiplication and Assignment `*=` Operator 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;`

C 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.

Division and Assignment `/=` Operator Syntax:

lValue /= rValue;

Division and Assignment `/=` Operator 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;`

Facebook
Twitter
Pinterest
LinkedIn

Top Technologies