by Dinesh Thakur Category: Operator

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;
   clrscr();
   A += 3;
   B *= 2;
   P -= 4;
   S /=5;
   T %=8;
   printf("A =%d\tB = %d\tP = %d\tS = %d\n", A, B, P, S);
   printf("T = %d\n", T );
}

Composite Assignment Operators in C



About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.SC (Computer Science), MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.