Lesson 19 of 60 – Increment and Decrement Operators in C
32%

Increment and Decrement Operators in C

Increment and decrement operators are used to increase or decrease the value of a variable by exactly one. They are commonly used with counters, loops, and calculations in C programs.

Note: The increment operator is ++ and the decrement operator is --. Both can be used in prefix or postfix form.

1. What is the Increment Operator?

The increment operator ++ increases the value of an object by one.

int number = 10;

number++;

printf("%d", number);

Output:

11

2. What is the Decrement Operator?

The decrement operator -- decreases the value of an object by one.

int number = 10;

number--;

printf("%d", number);

Output:

9

3. Increment Operator ++

The ++ operator is used when you want to increase a variable by one.

int count = 5;

count++;

printf("%d", count);

Output:

6

4. Decrement Operator --

The -- operator decreases a variable by one.

int count = 5;

count--;

printf("%d", count);

Output:

4

5. Increment is Similar to += 1

For an appropriate modifiable arithmetic object, incrementing by one can be written as number += 1.

int number = 10;

number += 1;

printf("%d", number);

Output:

11

The ++ operator provides a shorter way to increment by one.

6. Decrement is Similar to -= 1

For an appropriate modifiable arithmetic object, decrementing by one can be written as number -= 1.

int number = 10;

number -= 1;

printf("%d", number);

Output:

9

7. Prefix Increment

In prefix increment, the ++ operator is placed before the variable.

int number = 10;

++number;

printf("%d", number);

Output:

11

The variable is incremented before the statement continues.

8. Postfix Increment

In postfix increment, the ++ operator is placed after the variable.

int number = 10;

number++;

printf("%d", number);

Output:

11

When used as a standalone statement, prefix and postfix increment both increase the variable by one.

9. Prefix Decrement

In prefix decrement, the -- operator is placed before the variable.

int number = 10;

--number;

printf("%d", number);

Output:

9

10. Postfix Decrement

In postfix decrement, the -- operator is placed after the variable.

int number = 10;

number--;

printf("%d", number);

Output:

9

11. Prefix vs Postfix Increment

Prefix and postfix forms differ when the value of the expression itself is used.

int a = 5;
int b = ++a;

printf("a = %d\n", a);
printf("b = %d", b);

Output:

a = 6
b = 6

The prefix increment changes a before its value is assigned to b.

12. Postfix Increment Example

int a = 5;
int b = a++;

printf("a = %d\n", a);
printf("b = %d", b);

Output:

a = 6
b = 5

The old value of a is used for the expression, and then a is incremented.

13. Prefix vs Postfix Decrement

int a = 5;
int b = --a;

printf("a = %d\n", a);
printf("b = %d", b);

Output:

a = 4
b = 4

The decrement happens before the value is used in the expression.

14. Postfix Decrement Example

int a = 5;
int b = a--;

printf("a = %d\n", a);
printf("b = %d", b);

Output:

a = 4
b = 5

The old value is used for the expression, and then a is decremented.

15. Increment in a for Loop

#include <stdio.h>

int main()
{
    for (int i = 1; i <= 5; i++)
    {
        printf("%d ", i);
    }

    return 0;
}

Output:

1 2 3 4 5

The increment operator increases i after each loop iteration.

16. Decrement in a for Loop

#include <stdio.h>

int main()
{
    for (int i = 5; i >= 1; i--)
    {
        printf("%d ", i);
    }

    return 0;
}

Output:

5 4 3 2 1

The decrement operator decreases i after each iteration.

17. Increment with while Loop

int i = 1;

while (i <= 5)
{
    printf("%d ", i);
    i++;
}

Output:

1 2 3 4 5

The variable is incremented inside the loop body.

18. Decrement with while Loop

int i = 5;

while (i >= 1)
{
    printf("%d ", i);
    i--;
}

Output:

5 4 3 2 1

19. Incrementing a Counter

int count = 0;

count++;
count++;
count++;

printf("Count = %d", count);

Output:

Count = 3

Increment is commonly used for counters.

20. Decreasing a Counter

int count = 5;

count--;
count--;

printf("Count = %d", count);

Output:

Count = 3

Decrement is useful when a counter needs to move downward.

21. Incrementing User Input

#include <stdio.h>

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    number++;

    printf("After increment = %d", number);

    return 0;
}

If the user enters 10, the output will be After increment = 11.

22. Decrementing User Input

#include <stdio.h>

int main()
{
    int number;

    printf("Enter a number: ");
    scanf("%d", &number);

    number--;

    printf("After decrement = %d", number);

    return 0;
}

If the user enters 10, the output will be After decrement = 9.

23. Increment and Decrement with Conditions

int number = 10;

if (number > 5)
{
    number++;
}

printf("%d", number);

Output:

11

The value is incremented only when the condition is true.

24. Using Prefix Increment in an Expression

int number = 5;

printf("%d", ++number);

Output:

6

With prefix increment, the variable is incremented before its value is used by the expression.

25. Using Postfix Increment in an Expression

int number = 5;

printf("%d", number++);

printf("\n%d", number);

Output:

5
6

The expression uses the old value first, and then the variable is incremented.

26. Using Prefix Decrement in an Expression

int number = 5;

printf("%d", --number);

Output:

4

The variable is decremented before its value is used.

27. Using Postfix Decrement in an Expression

int number = 5;

printf("%d", number--);

printf("\n%d", number);

Output:

5
4

The old value is used first, and then the variable is decremented.

28. Common Increment and Decrement Mistakes

  • Confusing prefix and postfix behavior when the expression value is used.
  • Trying to increment a constant or other non-modifiable object.
  • Forgetting to update a loop counter.
  • Using decrement when an increment is required.
  • Writing complicated expressions with multiple modifications of the same variable.
  • Using ++ or -- on expressions that are not modifiable objects.

29. Complete Increment and Decrement Program

#include <stdio.h>

int main()
{
    int number = 10;

    printf("Original = %d\n", number);

    number++;
    printf("After increment = %d\n", number);

    number++;
    printf("After second increment = %d\n", number);

    number--;
    printf("After decrement = %d\n", number);

    number--;
    printf("After second decrement = %d\n", number);

    return 0;
}

Output:

Original = 10
After increment = 11
After second increment = 12
After decrement = 11
After second decrement = 10

30. Practice with Increment and Decrement

Practice the following programs:

  1. Increment a number by one.
  2. Decrement a number by one.
  3. Use prefix increment in an expression.
  4. Use postfix increment in an expression.
  5. Use prefix decrement in an expression.
  6. Use postfix decrement in an expression.
  7. Print numbers from 1 to 10 using increment.
  8. Print numbers from 10 to 1 using decrement.
  9. Create a counter using ++.
  10. Create a countdown using --.

Practice both prefix and postfix forms so that you understand when the variable is modified relative to the value produced by the expression.

📌 Key Points

  • ++ increases a variable by one.
  • -- decreases a variable by one.
  • Prefix increment is written as ++variable.
  • Postfix increment is written as variable++.
  • Prefix decrement is written as --variable.
  • Postfix decrement is written as variable--.
  • Prefix and postfix forms differ when their expression value is used.
  • Increment and decrement operators are commonly used in loops.
  • They can be used with appropriate modifiable arithmetic objects.
  • Avoid unnecessarily complex expressions that modify the same variable multiple times.

🧠 Quick Quiz

Question: Which operator is used to increase the value of a variable by one?