Assignment Operators in C

In this video tutorial we show Compound Assignment Operators in C programming language.

Related Read:
Simple Calculator Application In C

Assignment Operators

If a and c are the operands.

c += a; is equal to writing c = c + a;
c -= a; is equal to writing c = c – a;
c *= a; is equal to writing c = c * a;
c /= a; is equal to writing c = c / a;
c %= a; is equal to writing c = c % a;

Assignment Operator +=

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a = 20, c = 30;  
  7.   
  8.     c += a ; // c = c + a;  
  9.   
  10.     printf("%d\n\n", c);  
  11.   
  12.     return 0;  
  13. }  

Output:
50

Assignment Operator -=

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a = 20, c = 30;  
  7.   
  8.     c -= a ; // c = c - a;  
  9.   
  10.     printf("%d\n\n", c);  
  11.   
  12.     return 0;  
  13. }  

Output:
10

Assignment Operator *=

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a = 20, c = 30;  
  7.   
  8.     c *= a ; // c = c * a;  
  9.   
  10.     printf("%d\n\n", c);  
  11.   
  12.     return 0;  
  13. }  

Output:
600

Assignment Operator /=

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a = 2, c = 20;  
  7.   
  8.     c /= a ; // c = c / a;  
  9.   
  10.     printf("%d\n\n", c);  
  11.   
  12.     return 0;  
  13. }  

Output:
10

Assignment Operator %=

  1.    
  2. #include < stdio.h >  
  3.   
  4. int main()  
  5. {  
  6.     int a = 2, c = 20;  
  7.   
  8.     c %= a ; // c = c % a;  
  9.   
  10.     printf("%d\n\n", c);  
  11.   
  12.     return 0;  
  13. }  

Output:
0

Compound Assignment Operators in C Programming Language



YouTube Link: https://www.youtube.com/watch?v=-_ZBdT2Yxqo [Watch the Video In Full Screen.]


You can also learn about Logical and Relational Operators in C:
Relational Operators In C
Logical Operators In C

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert