Skip to content
StudyFame

Increment and Decrement Operators in C

Adding 1 to a variable is so common that C has a special operator for it: ++. There is also -- to subtract 1.

The tricky part, and a favourite interview question, is the difference between i++ and ++i. In this lesson you will understand it with simple step-by-step tables.

What You Will Learn

  • What ++ and -- do.
  • Pre-increment (++i) vs post-increment (i++).
  • Pre-decrement and post-decrement.
  • When the difference matters and when it doesn't.
  • Why expressions like i++ + ++i must be avoided.

Increment and Decrement Operators Explained

Exam definition: The increment operator (++) increases a variable's value by 1, and the decrement operator (--) decreases it by 1. Both are unary operators and have two forms: prefix (++x) and postfix (x++).

FormNameWhat happensIf x = 5: y = ...Then x
++xPre-incrementFirst increase, then usey = ++x → y = 66
x++Post-incrementFirst use, then increasey = x++ → y = 56
--xPre-decrementFirst decrease, then usey = --x → y = 44
x--Post-decrementFirst use, then decreasey = x-- → y = 54

Memory trick: Look at where the ++ is. ++x: the ++ comes first, so add first, then use. x++: x comes first, so use first, then add. In both cases, x ends up 1 bigger.

When does the difference matter?

  • On a line by itself (i++; or ++i;), there is no difference: both just add 1.
  • The difference matters only when the value is used in the same expression, like y = x++ or printf("%d", x++).

Syntax of ++ and --

++variable;    /* pre-increment  */
variable++;    /* post-increment */
--variable;    /* pre-decrement  */
variable--;    /* post-decrement */

They work only on variables: 5++ or (a + b)++ are errors.

Example 1: Pre and Post Increment Step by Step

#include <stdio.h>

int main(void)
{
    int x = 5, y;

    y = x++;     /* use 5, then x becomes 6 */
    printf("After y = x++ : y = %d, x = %d\n", y, x);

    y = ++x;     /* x becomes 7, then use 7 */
    printf("After y = ++x : y = %d, x = %d\n", y, x);

    y = x--;     /* use 7, then x becomes 6 */
    printf("After y = x-- : y = %d, x = %d\n", y, x);

    y = --x;     /* x becomes 5, then use 5 */
    printf("After y = --x : y = %d, x = %d\n", y, x);

    return 0;
}

Output

After y = x++ : y = 5, x = 6
After y = ++x : y = 7, x = 7
After y = x-- : y = 7, x = 6
After y = --x : y = 5, x = 5

Code Explanation

  1. y = x++;: y gets the old value 5, then x becomes 6.
  2. y = ++x;: x becomes 7 first, then y gets 7.
  3. y = x--;: y gets the old value 7, then x becomes 6.
  4. y = --x;: x becomes 5 first, then y gets 5.

How ++ Works Internally

  • x++ is like: temp = x; x = x + 1; and the expression's value is temp (the old value).
  • ++x is like: x = x + 1; and the expression's value is the new x.
  • Modern compilers produce the same fast code for both when the value is not used, so on its own line, choose either.

Avoid changing a variable twice in one expression

Expressions like i++ + ++i, i = i++ or printf("%d %d", i++, i++) change the same variable more than once without a clear order. This is undefined behaviour: different compilers give different answers, and none of them is "the right one". Never write such code, and if an exam asks for its output, the correct answer is "undefined".

Example 2: Using ++ and -- in a Real Program

#include <stdio.h>

int main(void)
{
    int tickets = 3;
    int sold = 0;

    printf("Tickets left: %d\n", tickets);

    tickets--;  sold++;
    printf("Sold 1. Left: %d, Sold: %d\n", tickets, sold);

    tickets--;  sold++;
    printf("Sold 1. Left: %d, Sold: %d\n", tickets, sold);

    printf("Next ticket number: %d\n", ++sold);
    printf("Sold count is now: %d\n", sold);

    return 0;
}

Output

Tickets left: 3
Sold 1. Left: 2, Sold: 1
Sold 1. Left: 1, Sold: 2
Next ticket number: 3
Sold count is now: 3

On their own lines, tickets-- and sold++ simply subtract and add 1. In the last printf, ++sold adds 1 before printing, so it prints 3.

Important Points to Remember

  • ++ adds 1, -- subtracts 1. Both are unary.
  • ++x (pre): change first, then use.
  • x++ (post): use first, then change.
  • On a line by itself, x++ and ++x do the same thing.
  • Changing a variable twice in one expression is undefined behaviour.

Common Mistakes Beginners Make

MistakeWhat happensFix
5++ or (a+b)++Error: lvalue requiredUse on a variable only
i = i++;Undefined behaviourJust write i++;
printf("%d %d", x++, x);Undefined behaviourIncrement on a separate line
x+ + with a spaceSyntax errorNo space: x++

Increment and Decrement Practice Questions

  1. What is the difference between x++ and ++x?
  2. If a = 10, what are a and b after b = a++;?
  3. If a = 10, what are a and b after b = --a;?
  4. Is there any difference between i++; and ++i; on their own lines?
  5. What is the output of i = 5; i = i++ + ++i;?
Show answers
  1. x++ uses the old value then adds 1; ++x adds 1 first then uses the new value.
  2. b = 10, a = 11.
  3. a = 9, b = 9.
  4. No, both add 1.
  5. Undefined behaviour: i is changed twice in one expression, so there is no correct fixed answer.

Coding Exercise

Start with int count = 10;. Print the result of count++, then count, then ++count, each on a separate printf line, and explain the three numbers.

Show solution
#include <stdio.h>

int main(void)
{
    int count = 10;

    printf("count++ gives %d\n", count++);
    printf("count is now %d\n", count);
    printf("++count gives %d\n", ++count);
    return 0;
}
count++ gives 10
count is now 11
++count gives 12

count++ prints the old value 10 and then makes it 11. ++count makes it 12 and then prints 12.

Quick Quiz: i++ vs ++i (MCQ)

  1. If x = 3, what is y after y = x++;?

    A) 3   B) 4   C) 2   D) 5

  2. If x = 3, what is y after y = ++x;?

    A) 3   B) 4   C) 2   D) 5

  3. Increment and decrement operators are:

    A) Unary   B) Binary   C) Ternary   D) Logical

  4. Which is invalid?

    A) a++   B) --a   C) 10++   D) ++a

  5. Output of int a = 5; printf("%d", a--);

    A) 4   B) 5   C) 6   D) Undefined

Show answers
  1. A: 3 (x becomes 4 after).
  2. B: 4.
  3. A: unary.
  4. C: 10++
  5. B: 5 (a becomes 4 after printing).

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

++ / --Add 1 / subtract 1 (unary)
++x (pre)Change first, then use
x++ (post)Use first, then change
x = 5, y = x++y = 5, x = 6
x = 5, y = ++xy = 6, x = 6
On its own lineNo difference
i++ + ++iUndefined behaviour

Frequently asked questions

What is the difference between i++ and ++i?
i++ returns the old value then adds 1; ++i adds 1 first and returns the new value.
Which is faster, i++ or ++i?
For simple variables, the same: compilers produce identical code when the value isn't used.
What is the output of i = i++?
Undefined behaviour: i is modified twice without a defined order.
Can ++ be used on a constant?
No. It needs a variable (lvalue); 5++ is an error.

Remember

  • ++ first = add first; x first = use first.
  • Never change the same variable twice in one expression.

Next Topic

Next lesson: Bitwise Operators in C. You will learn operators that work on the individual bits of a number.