0
0
Cprogramming~5 mins

Assignment operators in C

Choose your learning style9 modes available
Introduction

Assignment operators let you store values in variables easily. They help update or change values step by step.

When you want to save a number or text in a variable.
When you want to add or subtract a value from a variable quickly.
When you want to multiply or divide a variable by a number and save the result.
When you want to update a variable based on its current value.
Syntax
C
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.

Examples
Assign 5 to variable x.
C
int x = 5;
Add 3 to x and save the new value in x.
C
x += 3;  // same as x = x + 3;
Multiply x by 2 and save the result in x.
C
x *= 2;  // same as x = x * 2;
Set x to the remainder when x is divided by 4.
C
x %= 4;  // same as x = x % 4;
Sample Program

This program shows how assignment operators change the value of x step by step.

C
#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;
}
OutputSuccess
Important Notes

Remember that /= with integers does division and keeps only the whole number part.

The %= operator works only with integers and gives the remainder.

Summary

Assignment operators store or update values in variables.

They combine math and assignment to make code shorter and clearer.

Common ones include =, +=, -=, *=, /=, and %=.