MCS-201 · Unit 2

Data Types, Operators and Expressions

Section 7 of 11

Block 1 · Unit 2Data Types, Operators and Expressions10 min · Section 7 of 11

2.7Increment and Decrement Operators

Covers IGNOU MCS-201 §2.13

Adding or subtracting 1 is so common that C has special operators for it. They are short and convenient, but their prefix and postfix forms behave differently inside larger expressions, and using them carelessly can make a program’s behaviour undefined.

Prefix and postfix forms

The increment operator ++ adds 1 to a variable; the decrement operator -- subtracts 1.

ExpressionEffect on aValue used in the expression
++a (pre-increment)a becomes a + 1The new value
a++ (post-increment)a becomes a + 1The old value
--a (pre-decrement)a becomes a - 1The new value
a-- (post-decrement)a becomes a - 1The old value

Used alone as a statement, a++;, ++a; and a = a + 1; all have the same effect. The difference appears only when the value is used in a larger expression.

Undefined behaviour: changing a variable twice

If an expression modifies a variable more than once, or modifies it and also reads it elsewhere in the same expression, the C standard does not define the result. Different compilers, or the same compiler with different settings, may give different answers. This is called undefined behaviour.

C · unsafe.c
#include <stdio.h>

int main(void)
{
    int a = 1, b = 2, c = 3, k;

    k = (a++) * (++b) + ++a - --c;   /* a is modified twice */
    printf("a=%d b=%d c=%d k=%d\n", a, b, c, k);
    return 0;
}
Compiler output
gcc:   warning: operation on 'a' may be undefined [-Wsequence-point]
clang: warning: multiple unsequenced modifications to 'a' [-Wunsequenced]
Output (gcc 13 and clang 18, x86-64)
a=3 b=3 c=2 k=4

Safe rule: in one expression, apply ++ or -- to a variable at most once, and do not use that variable anywhere else in the same expression.

Key takeaways

  • ++ adds 1 and -- subtracts 1; both are unary.
  • Prefix uses the new value; postfix uses the old value.
  • Alone as statements, a++, ++a and a = a + 1 are equivalent.
  • Modifying a variable twice in one expression is undefined behaviour; compilers warn about it.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

After int x = 5; int y = x++;, what are x and y?

Show answer

Answer: C. x = 6, y = 5

Postfix gives the old value 5 to y, then increments x to 6. Prefix ++x would give 6 to both.

Question 2 · multiple choice

After int x = 5; int y = --x * 2;, what is y?

Show answer

Answer: A. 8

--x decrements x to 4 first, so y = 4 * 2 = 8. Using the old value would give 10.

Question 3 · multiple choice

Which statement has undefined behaviour?

Show answer

Answer: D. i = i++ + 1;

i = i++ + 1; modifies i twice (by ++ and by =) in one expression. The others modify i once.

Question 4 · multiple choice

What does this program print?

C · incdec_quiz.c
#include <stdio.h>

int main(void)
{
    int a = 4, b = 7, c;
    c = a++ + --b;
    printf("%d %d %d\n", a, b, c);
    return 0;
}
Show answer

Answer: B. 5 6 10

a++ contributes 4 (then a becomes 5), and --b makes b 6 and contributes 6, so c = 4 + 6 = 10. Using the new value of a would give 11.

Question 5 · multiple choice

As standalone statements, which pair has exactly the same effect?

Show answer

Answer: A. count++; and count = count + 1;

On its own, count++; simply adds 1. The prefix/postfix difference matters only when the value is used inside a larger expression.

Question 6 · short answer

Trace int p = 3, q = 8, r; r = p++ * 2 + --q; and give the final values of p, q and r.

Show worked answer

p++ contributes the old value 3 and then p becomes 4. --q decrements q to 7 and contributes 7. So r = 3 * 2 + 7 = 6 + 7 = 13. Final values: p = 4, q = 7, r = 13. Each variable is modified only once, so the result is well defined.

↑ ↓ move↵ openTab switch type