0Pricing
C Academy · Lesson

Assignment and Increment

=, +=, ++, --.

Assignment and Increment is a free C Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Assignment Operator

The = operator stores the value on its right into the variable on its left. It is not equality.

#include <stdio.h>
int main(void) {
    int x;
    x = 10;
    printf("x = %d\n", x);
    return 0;
}

Assignment Is an Expression

In C, an assignment produces a value (the assigned value). This lets you chain assignments.

#include <stdio.h>
int main(void) {
    int a, b, c;
    a = b = c = 5;
    printf("%d %d %d\n", a, b, c);
    return 0;
}

Compound Assignment

Operators like += combine arithmetic with assignment. x += 3 means x = x + 3.

#include <stdio.h>
int main(void) {
    int x = 10;
    x += 5;
    printf("x = %d\n", x);
    return 0;
}

All Compound Forms

The same shorthand exists for every arithmetic operator: -=, *=, /=, %=.

#include <stdio.h>
int main(void) {
    int x = 20;
    x -= 4;
    x *= 2;
    x %= 7;
    printf("x = %d\n", x);
    return 0;
}

The Increment Operator

++ adds one to a variable. x++ is shorter than x = x + 1.

#include <stdio.h>
int main(void) {
    int count = 0;
    count++;
    count++;
    printf("count = %d\n", count);
    return 0;
}

The Decrement Operator

-- subtracts one from a variable.

#include <stdio.h>
int main(void) {
    int lives = 3;
    lives--;
    printf("lives = %d\n", lives);
    return 0;
}

Post-Increment

With post-increment x++, the current value is used first, then x is increased.

#include <stdio.h>
int main(void) {
    int x = 5;
    int y = x++;
    printf("x = %d, y = %d\n", x, y);
    return 0;
}

Pre-Increment

With pre-increment ++x, x is increased first, then its new value is used.

#include <stdio.h>
int main(void) {
    int x = 5;
    int y = ++x;
    printf("x = %d, y = %d\n", x, y);
    return 0;
}

Increment in Loops

Increment shines in loops, where it advances a counter each pass.

#include <stdio.h>
int main(void) {
    for (int i = 0; i < 3; i++)
        printf("i = %d\n", i);
    return 0;
}

Avoid Confusing Expressions

Using a variable multiple times with ++ in one expression like i = i++ + ++i is undefined behavior. Keep increments simple.

#include <stdio.h>
int main(void) {
    int i = 0;
    i++;
    i++;
    printf("i = %d\n", i);
    return 0;
}

Putting It Together

Combine compound assignment and increment to track a running total.

#include <stdio.h>
int main(void) {
    int total = 0;
    for (int i = 1; i <= 5; i++)
        total += i;
    printf("total = %d\n", total);
    return 0;
}

Quick Check

Test your understanding of assignment and increment.

Recap: Assignment and Increment

You learned assignment and the increment family.

  • = stores a value and is itself an expression
  • Compound operators like += are handy shorthands
  • ++ and -- change by one
  • Post vs pre differs in when the change is visible
  • Avoid modifying one variable twice in a single expression

Frequently asked questions

Is the “Assignment and Increment” lesson free?

Yes — the full text of “Assignment and Increment” is free to read here on the web, and the C Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C Academy course, upgrade to CoddyKit PRO.

What will I learn in “Assignment and Increment”?

=, +=, ++, --. You practise C Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C Academy?

No prior experience is required. C Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Assignment and Increment” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C Academy lesson?

Yes. Every C Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Arithmetic Operators
  2. Relational and Logical
  3. Assignment and Increment
  4. Operator Precedence
← Back to C Academy