Nearby lessons

28 of 124

C - Assignment Operators

Assignment Operators is one of the foundational topics in C programming. This lesson explains Assignment Operators (= += -= *= /= %=) and Program: every shortcut operator with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Assignment Operators (= += -= *= /= %=)

The = assigns a value. The shortcut operators combine assignment with an operation — a += 5 means a = a + 5.

OperatorMeansExample (a=10)
=assigna = 5; → a is 5
+=a = a + valuea += 5; → a is 15
-=a = a - valuea -= 5; → a is 5
*=a = a * valuea *= 2; → a is 20
/=a = a / valuea /= 5; → a is 2
%=a = a % valuea %= 3; → a is 1

Program: every shortcut operator

Example02
CCode Cell
1#include <stdio.h>
2 
3void main()
4{
5 int a = 10;
6 
7 a += 5; printf("after a += 5 : %d\n", a); // 15
8 a -= 3; printf("after a -= 3 : %d\n", a); // 12
9 a *= 2; printf("after a *= 2 : %d\n", a); // 24
10 a /= 4; printf("after a /= 4 : %d\n", a); // 6
11 a %= 4; printf("after a %%= 4 : %d\n", a); // 2
12}
Output

after a += 5 : 15
after a -= 3 : 12
after a *= 2 : 24
after a /= 4 : 6
after a %= 4 : 2
      
📝 Key Takeaways
  • Operators: arithmetic (+ - * / %), relational (== != < > <= >=), logical (&& || !), assignment (= += -= *= /= %=), ++ --, ternary ?:.
  • Each operator family has its own complete program — copy and run one at a time.
  • Integer division drops decimals: 5/2 = 2; use 5.0/2 for 2.5.
  • % gives the remainder and works only on integers.
  • a++ uses then increases; ++a increases then uses.
  • Ternary: condition ? value1 : value2.
  • Use brackets to control the order of evaluation.

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1