C - Using compound assignment operators

Introduction

The following table lists the C Math Compound Assignment Operators.

Operator Function Shortcut for Example
+=Addition x=x+n x+=n
-= Subtraction x=x-n x-=n
*=Multiplicationx=x*n x*=n
/=Division x=x/n x/=n
%= Modulox=x%n x%=n

For example:

myVariable=myVariable+10; 

This statement increases the value of variable myVariable by 10.

In C, you can write the same statement by using an assignment operator as follows:

myVariable+=10; 

Demo

#include <stdio.h> 

int main() /*from ww w.j  a v  a2s  .c  o  m*/
{ 
    float myVariable; 

    myVariable=501; 
    printf("myVariable = %.1f\n",myVariable); 
    myVariable=myVariable+99; 
    printf("myVariable = %.1f\n",myVariable); 
    myVariable=myVariable-250; 
    printf("myVariable = %.1f\n",myVariable); 
    myVariable=myVariable/82; 
    printf("myVariable = %.1f\n",myVariable); 
    myVariable=myVariable*4.3; 
    printf("myVariable = %.1f\n",myVariable); 
    return(0); 
}

Result

When you use the assignment operator, keep in mind that the = character comes last.

Related Topic