What is C Assignment Operators Guide- Best C Language Course For Beginners

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

Free C Programming Course • Chapter 4 • Lecture 4

C Assignment Operators Guide

The = operator, chained assignment and the compound operators += -= *= /= %=, with the traps that catch beginners. Part of our free C tutorial series.

1
Simple assignment
5
Arithmetic compound forms
4
Working examples

Every program stores values, and the assignment operator is how it happens. This lecture of our free C programming course explains = as an operator that produces a value, shows how chained assignment works, and introduces the compound operators that make code shorter and safer. If you want a free C tutorial that connects these ideas to real firmware habits, keep reading.

What You Will Learn in This Free C Course

  • The rule for the left and right side of an assignment
  • Assignment expression versus assignment statement
  • How chained assignment like x = y = z = 20 is evaluated
  • All five arithmetic compound assignment operators
  • Why x *= a + b is not x = x * a + b
  • What happens when a float is assigned to an int
  • The classic = versus == bug

Prerequisites

Lectures 1 to 3 of this chapter and a working understanding of variables and the arithmetic operators.

The Simple Assignment Operator

The assignment operator = stores a value in a variable. It is used in assignment expressions and assignment statements. The rule is simple: the left operand must be a modifiable variable (something with a memory location), while the right operand can be a variable, a constant or any expression. The right side is evaluated first, then its value is stored in the left side.

AssignmentMeaning
level = 8Constant 8 is stored in level
total = a + b - 2Value of the expression is stored in total
copy = originalValue of original is copied into copy
8 = levelCompile error: a constant cannot be on the left
Evaluation order of: total = a + b – 2
1. Read a and b 2. Compute a + b – 2 3. Store result in total 4. Whole expression has that value

Expression Versus Statement

In C, x = 8 is an expression whose value is 8. Put a semicolon after it and it becomes an assignment statement: x = 8;. Because assignment is an expression, it can sit inside a bigger expression, for example r = (x = 8) + 2.

Multiple Assignment (Chaining)

Assignment evaluates right to left. In x = y = z = 20, first z = 20 runs and yields 20, which is assigned to y, which yields 20, assigned to x. All three hold 20, and the whole expression has the value 20.

Right-to-left evaluation of x = y = z = 20
z = 20 (value 20) y = 20 (value 20) x = 20 (value 20)

Compound Assignment Operators in C

When the variable on the left also appears on the right, you can avoid writing it twice with a compound assignment operator. For example x = x + 5 becomes x += 5.

Compound formEquivalent to
x += 5x = x + 5
x -= 5x = x - 5
x *= 5x = x * 5
x /= 5x = x / 5
x %= 5x = x % 5

Besides saving typing, compound operators evaluate the left operand only once, which matters when the left side is a complicated expression such as an array element with a function call as index. In embedded code you will later meet |=, &=, ^=, <<= and >>= for setting and clearing hardware register bits.

Precedence trap: the whole right side is evaluated first. So x *= a + b means x = x * (a + b), not x = x * a + b.

Hands-On Assignment Operator Examples

Compile with gcc -Wall -Wextra -std=c11 file.c -o out.

Example 1: Assignment and Swapping Two Values

/* cp4_4_ex1.c */
#include <stdio.h>

int main(void)
{
    int a = 3, b = 9, temp;

    printf("before: a=%d b=%d\n", a, b);
    temp = a;
    a = b;
    b = temp;
    printf("after : a=%d b=%d\n", a, b);
    return 0;
}
before: a=3 b=9
after : a=9 b=3

Assignment overwrites the old value. Without temp the original a would be lost, so swapping needs a third variable.

Example 2: Chained Assignment and Expression Value

/* cp4_4_ex2.c */
#include <stdio.h>

int main(void)
{
    int x, y, z, r;

    x = y = z = 20;
    printf("x=%d y=%d z=%d\n", x, y, z);

    r = (x = 8) + 2;
    printf("x=%d r=%d\n", x, r);
    return 0;
}
x=20 y=20 z=20
x=8 r=10

Example 3: Every Compound Operator in Sequence

/* cp4_4_ex3.c */
#include <stdio.h>

int main(void)
{
    int score = 10;

    score += 5;  printf("after += 5 : %d\n", score);
    score -= 3;  printf("after -= 3 : %d\n", score);
    score *= 2;  printf("after *= 2 : %d\n", score);
    score /= 5;  printf("after /= 5 : %d\n", score);
    score %= 3;  printf("after %%= 3 : %d\n", score);
    return 0;
}
after += 5 : 15
after -= 3 : 12
after *= 2 : 24
after /= 5 : 4
after %= 3 : 1

Example 4: Precedence and Narrowing Traps

/* cp4_4_ex4.c */
#include <stdio.h>

int main(void)
{
    int x = 10;
    x *= 2 + 3;
    printf("x = %d\n", x);

    int n = 7.9;
    printf("n = %d\n", n);

    int total = 10;
    total += 2.7;
    printf("total = %d\n", total);
    return 0;
}
x = 50
n = 7
total = 12

Line one: 2 + 3 is 5, so x = 10 * 5 = 50. Line two: assigning a double to an int truncates, it does not round. Line three: total + 2.7 is 12.7 in floating-point, which is truncated to 12 when stored back into the int.

The = Versus == Bug

The most famous assignment mistake in C is writing = where you meant the equality test ==:

int mode = 0;
if (mode = 1) {      /* assigns 1, condition is always true */
    printf("enabled\n");
}
$ gcc -Wall -Wextra bug.c
bug.c:4:13: warning: suggest parentheses around assignment used as truth value [-Wparentheses]

This compiles, overwrites mode, and the branch always runs. Always build with -Wall so the compiler warns you.

Real-World Applications

  • Embedded: accumulating sensor readings with sum += reading, decrementing retry counters, updating hardware registers.
  • Linux programs: accumulating byte counts, advancing offsets with offset += n.
  • General: running totals, scores, balances.

Common Mistakes and Troubleshooting

MistakeSymptomFix
= instead of == in a conditionCondition always true or falseUse ==; build with -Wall
Assuming x *= a + b multiplies only by aWrong totalsRemember the right side is grouped
Assigning a float to an intFraction silently lostCast or round deliberately
Chaining with different typesSurprising truncation along the chainAssign separately
Using an uninitialised variable on the rightRandom valuesInitialise at declaration

Best Practices

  • Initialise variables when you declare them.
  • Use compound operators when updating a variable from its own value; it reads clearly.
  • Avoid cramming assignments inside conditions unless you add extra parentheses to show intent.
  • Keep chained assignment to variables of the same type.
  • Use const for values that must never be reassigned; the compiler then rejects accidental assignment.

Key Takeaways

  • The left side of = must be a modifiable variable.
  • Assignment is an expression with a value and evaluates right to left.
  • += -= *= /= %= are shorthand for updating a variable from itself.
  • The entire right side is computed before the compound operation.
  • Assigning a floating value to an int truncates.

Conclusion

Assignment looks trivial yet drives every program you will write. You can now read and write simple, chained and compound assignments and avoid the classic traps. Increment and decrement operators come next in this free C programming course, completing the family of operators that modify variables.

Quick Quiz

  1. What is the value of the expression x = 8?
    Show answer8.
  2. What does k %= 5 mean?
    Show answerk = k % 5.
  3. If x = 4, what is x after x *= 3 + 1?
    Show answer16.
  4. Is 10 = n legal?
    Show answerNo. The left side must be a variable.
  5. What does int n = 5.8; store?
    Show answer5.

Interview Questions

  1. Explain the difference between an assignment expression and an assignment statement.
  2. How is a = b = c = 0 evaluated?
  3. What bug does if (x = 5) cause, and how can the compiler help?
  4. Why does x *= a + b differ from x = x * a + b?
  5. Which compound operators are common in embedded register code?

Frequently Asked Questions

Is this free C programming course in order?

Yes. Chapter 4 goes from arithmetic operators to assignment and then to increment, relational, logical and bitwise operators.

What is the difference between = and ==?

The single equals stores a value; the double equals compares two values and gives 1 or 0.

What is a compound assignment operator?

A shorthand such as += that applies an operation to a variable and stores the result back in it.

Can I assign one variable to several in one line?

Yes, a = b = c = 0; works because assignment evaluates right to left.

Does assigning a float to an int round the number?

No. It truncates toward zero.

Where can I find more free C tutorial lectures?

Browse the course index of this free C programming course for every chapter, quiz and interview question set.

Keep Learning with the Free C Programming Course

Next: increment and decrement operators, prefix versus postfix, and why they are unary operators.

Need Help or More Information?

Questions about this free C programming course, embedded C training or career guidance? Get in touch.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *