How to Addition assignment operator in C ?
The addition assignment operator in C is "+=". It is used to add the value on the right-hand side of the operator to the variable on the left-hand side and store the result in the same variable.
For example, consider the following code:
int x = 5;
x += 3;
In this code, the value of x
is first initialized to 5. Then, the "+=" operator is used to add 3 to x
, resulting in x
being updated to 8.
Another example:
int a = 10;
int b = 5;
a += b;
In this code, the value of a
is first initialized to 10 and the value of b
is initialized to 5. Then, the "+=" operator is used to add the value of b
(which is 5) to a
, resulting in a
being updated to 15.
It is important to note that the addition assignment operator can be used with variables of different data types, as long as the result of the addition can be stored in the left-hand side variable without loss of precision. For example:
float f = 3.14;
int i = 2;
f += i;
In this code, the value of f
is first initialized to 3.14 and the value of i
is initialized to 2. Then, the "+=" operator is used to add the value of i
(which is 2) to f
, resulting in f
being updated to 5.14.