C Programming

How to Use Assignment Operator in C

This article will focus on Assignment Operators in C programming language. The assignment operator that is most frequently used is =. Additionally, Binary Operators include Assignment Operators. They have the lowest precedence level as compared to other operators, and they link from right to left. Assignment operators are used in the programming language C to assign a variable to its value. The assignment operator’s left-side parameter is a variable, and its right-side parameter is a value. To prevent a warning from the compiler, the item on the left must be of the same data type as the one on the right. Let’s talk about the various assignment operators, namely =, +=, -=, /=, *=, and %=.

Format

In the snippet below, we have an example of the simplest assignment operator in C programming, where we simply assign a numeric value to the integer; this helps us to explain the general format of the assignment operator.

#include <stdio.h>

int main(){
    int n;
    n = 5;

    printf("n = %d\n", n);
    return 0;
}
n = 5

Example # 01

The first example is the Simple assignment operator. By using the operator, the appropriate operand is reassigned to the left operand. There is only one straightforward assignment operator; “=”. Left operand = Right operand is the general syntax. The integer “a” (left operand of the simple assignment operator) is assigned the amount 5 in the case under (right operand of the simple assignment operator). The same goes for b, as well as c, where c gets assigned the sum of “a” and “b”. The final result is c=10, meaning c is assigned the value 10 with the help of this operator.

#include <stdio.h>

int main(){
    int a,b,c;
    a = 5;
    b = 5;
    c = a+b;

    printf("c = %d\n", c);
    return 0;
}
n = 10

Example # 02

The second example is the first Compound Assignment Operator called the Addition Assignment Operator “+=”. Imagine a much simpler version to comprehend this. Consider: a = a + 5. Here, what we are doing is that we add 5 to variable a, and then whatever result is achieved that is assigned to variable a. In the same way, what the line a += b is doing is that it is adding b to the value a and then assigning the result to the variable a. The variable b remains unchanged (b=10) as its value is not being changed; only the variable a’s value has been incremented by adding the value of b to it with the help of +=. We have found a that has been assigned with the value 15.

#include <stdio.h>

int main(){
    int a,b;
    a = 5;
    b = 10;
    a += b;

    printf("a = %d\n", a);
    printf("b = %d\n", b);
    return 0;
}
a = 15
b = 10

Example # 03

The third example is the Subtraction Assignment Operator “-=”. In this operator, the right operand is subtracted from the left operand and then equated to the left operand. This is just like saying a = a – 5. Here, we subtract 5 from a, then assign it to a. Similarly, the code below shows that b (with value 10) is being subtracted from a (with value 15) and then assign the result to a (making it have value 5). The value of b remains unchanged as the operator only assigns a value to the right operand while leaving the left operand’s values the same.

#include <stdio.h>

int main(){
    int a,b;
    a = 15;
    b = 10;
    a -= b;

    printf("a = %d\n", a);
    printf("b = %d\n", b);
    return 0;
}
a = 5
b = 10

Example # 04

The fourth example is the Multiplication Assignment Operator “*=”. The main operand is multiplied by the left argument and then matched to the left operand using this operator. A simpler low-level form of this would simply be a = a * 5, where the value of the variable a is multiplied by the value 5, and then the result is assigned to the value a itself. In the same way, the example below shows that the variable a (left operand) with value 15 is being assigned the result of the multiplication of the value of b (right operand), which is 10 with the value of a; thus, making the final result 150 being assigned to variable a. Again, the value of variable b remains unchanged.

#include <stdio.h>

int main(){
    int a,b;
    a = 15;
    b = 10;
    a *= b;

    printf("a = %d\n", a);
    printf("b = %d\n", b);
    return 0;
}
a = 150
b = 10

Example # 05

The next example is called the Division Assignment Operator “/=”. This operator allows the left operator to be equal to the result of the division of the left operand by the right operand. This is just like saying a = a / 5. Here, we divide a by 5, then assign it to a. Similarly, the code below shows that b (with value 10) is dividing a (with value 50) and then assigning the result to a (making it have value 5). The value of the variable b remains unchanged as the division operator, like any assignment operator, only assigns a value to the right operand while keeping the value of the left operand the same.

#include <stdio.h>

int main(){
    int a,b;
    a = 50;
    b = 10;
    a /= b;

    printf("a = %d\n", a);
    printf("b = %d\n", b);
    return 0;
}
a = 5
b = 10

Example # 06

The sixth and final example is the operator called Modulus Assignment Operator “%=”. This operator assigns the left operand the value obtained by taking the modulo of the left operand and right operand. The line a %= b is equivalent to saying a = a % b, where b can hold any value as well. In the example below, b holds the value 10 through a simple assignment operator, and a holds 55. Then, the modulus assignment operator finds the remainder of modulo of a and b, which is 5 in this case, and assigns it to the left operand, “a.” As usual, the right operand “b” stays unchanged with a value 10 as it is not being assigned a different value.

#include <stdio.h>

int main(){
    int a,b;
    a = 55;
    b = 10;
    a %= b;

    printf("a = %d\n", a);
    printf("b = %d\n", b);
    return 0;
}
a = 5
b = 10

Conclusion

In order to assign the outcome of an expression to a variable, it is a good choice to make use of assignment operators. In the programming language C, there are two different kinds of assignment operators. The “=” sign is the basic assignment operator. Furthermore, the Compound Assignment operators are simple to use and eliminate the need for repetitive writing on the part of the left operand. Other programming languages, such as C++, also function in the same way. We implemented multiple examples of the different types of assignment operators in the C programming language in this article.

About the author

Linux Wolfman

Linux Wolfman is interested in Operating Systems, File Systems, Databases and Analytics and always watching for new technologies and trends. Reach me by tweeting to @linuxhint and ask for the Wolfman.