Arithmetic operators may be combined with the assignment operator to obtain composite assignment operators. An arithmetic operator
is written first followed by the assignment operator but not vice versa.
In all the composite operators there should not be any blank space between the symbols.
For instance, in+=
if we give space between+ and= (suchas+ =),
it may result in an error.
The statement A += 6; implies add 6 to the present value of A and assign the result to A'. So, it is equivalent to the following statement:
A= A + 6;
Similarly, the statement A /= 4; is equivalent to the following statement:
A= A/4;
Other composite operators can be similarly dealt with.
Illustrates the application of composite assignment operators.
#include <stdio.h>
void main ()
{
int A=5, B=10, P=8, S = 20, T = 30;
A += 3;
B *= 2;
P -= 4;
S /=5;
T %=8;
printf("A =%d\nB = %d\n", A, B);
printf("P=%d\nS= %d\n", P, S);
printf("T = %d\n", T );
}
Output:
A =8
B = 20
P = 4
S = 4
T = 6
0 comments:
Post a Comment