Meaning of the Plus-Equal-To Operator in the C Programming Language
The plus equal to operator in the C programming language is used for variable assignment in a way that the value of the variable is updated by adding something to the current value of the variable. You will be able to understand this further by going through the following shared examples.
Using the Plus-Equal-To Operator in C
To use the plus-equal-to operator in the C programming language, you have to understand the following two examples:
Example 1: Simple Variable Assignment
In this example, we will use the plus-equal-to operator for a simple variable assignment. This is shown in the following sample script:
In this example, we first declare a variable “x”, assign to it the value “22”, and print it on the terminal. Then, we declare a variable “y”, equalize it to the variable “x”, and print the variable “y” on the terminal. After that, we use the plus-equal-to operator to update the value of the variable “y” and print it on the terminal.
Then, to compile this script, we use the following command:
To run this script, we use the following command:
The result of this script is shown in the following image. From here, you can see that the value of “x” is printed on the terminal. Then, since we copied the same value to the variable “y”, the very same value is printed on the terminal. However, after that, the updated value of the variable “y” is printed on the terminal which is calculated after adding the value of “x” to the current value of “y”, i.e. 22 + 22 = 44.
Example 2: Variable Assignment Within a Loop
In this example, we will take a look at another scenario in which we use the plus-equal-to operator within a loop in which the value of a variable is updated in every iteration of the loop. The C script that depicts this is shown in the following image:
In this script, we declare a variable “x” and assign to it the value of “22”. Then, we use a “for” loop which works for 5 iterations. Within this loop, we constantly keep adding the value of the iterator to the current value of the variable “x” in every iteration along with printing the current value on the terminal.
The output of this program is shown in the following image:
Conclusion
In this guide, we discussed the plus-equal-to operator of the C programming language. After going through the examples shared in this article, you will easily be able to understand the working of this particular assignment operator in C.