C Assignment Operators

Beginner
6 min read
Updated: Nov 2026
1 example

What Is Assignment Operators?

The = operator stores a value. Compound assignment operators combine an operation with assignment: x += 5 means x = x + 5 but evaluates x once. Assignment is also an expression whose value is the stored value.

Remember
x = 10;
x += 5;   x -= 3;   x *= 2;
x /= 4;   x %= 3;
x <<= 1;  x &= 0xF;  x |= 1;  x ^= 2;

Cheat Sheet

Keep these facts handy while you work with Assignment Operators in C.

TopicDetail
Simple=
Arithmetic+= -= *= /= %=
Bitwise&= |= ^= <<= >>=
Chaininga = b = 0; assigns right to left

C Examples

Compile each example with gcc file.c -std=c11 -Wall -o out. Next: Increment and Decrement.

Example 1 — Compound assignment chain

Follow n through each update.

C
#include <stdio.h>

int main(void) {
    int n = 10;

    n += 5;  printf("+= 5  -> %d\n", n);
    n -= 3;  printf("-= 3  -> %d\n", n);
    n *= 2;  printf("*= 2  -> %d\n", n);
    n /= 5;  printf("/= 5  -> %d\n", n);
    n %= 3;  printf("%%= 3  -> %d\n", n);
    n <<= 3; printf("<<= 3 -> %d\n", n);
    return 0;
}

How It Works

  1. 24 / 5 is integer division, giving 4.
  2. 4 % 3 leaves 1.
  3. 1 <<= 3 shifts the bit left three places to 8.

Edge Cases & Pitfalls

Check these before relying on Assignment Operators in real programs.

=

Assignment in a condition

while (c = getchar()) compiles but is easy to misread. Wrap it: while ((c = getchar()) != EOF).

narrow

Silent narrowing

char c = 300; or int x = 3.9; loses information without an error.

Key Takeaways

  • Shorthand: x op= y is x = x op y.
  • Chain: assignment associates right to left.
  • Careful: = is not ==.

One line: The = operator stores a value.

Frequently Asked Questions

The result is the same, but x is evaluated only once with +=, which matters when x has side effects such as arr[i++].
Yes. a = b = c = 0 assigns 0 to c, then b, then a, because = associates from right to left.
Continue with "C Increment and Decrement" next. Use ++ and -- in prefix and postfix form. The C Operators hub lists every topic in order.

Next: Increment and Decrement

Use ++ and -- in prefix and postfix form.

Continue →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful