Introduction
Assignment operators let you store values in variables easily. They help update or change values step by step.
Assignment operators let you store values in variables easily. They help update or change values step by step.
variable = value; variable += value; variable -= value; variable *= value; variable /= value; variable %= value;
The = operator assigns a value to a variable.
Operators like += combine assignment with math to update the variable.
int x = 5;
x += 3; // same as x = x + 3;
x *= 2; // same as x = x * 2;
x %= 4; // same as x = x % 4;
This program shows how assignment operators change the value of x step by step.
#include <stdio.h> int main() { int x = 10; printf("Initial x = %d\n", x); x += 5; printf("After x += 5: %d\n", x); x -= 3; printf("After x -= 3: %d\n", x); x *= 2; printf("After x *= 2: %d\n", x); x /= 4; printf("After x /= 4: %d\n", x); x %= 3; printf("After x %%= 3: %d\n", x); return 0; }
Remember that /= with integers does division and keeps only the whole number part.
The %= operator works only with integers and gives the remainder.
Assignment operators store or update values in variables.
They combine math and assignment to make code shorter and clearer.
Common ones include =, +=, -=, *=, /=, and %=.
x by 5 and assign the result back to x?int a = 10;
a += 5;
a -= 3;
printf("%d", a);
int x = 7;
x +== 3;
printf("%d", x);
result? int result = 20;
result /= 4;
result *= 3;
result %= 5;
result += 2;
printf("%d", result);
Pass the test and this pattern moves unknown