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.
++ and the
decrement operator is --. Both can be used in prefix or postfix
form.
The increment operator ++ increases the value of an object by
one.
int number = 10;
number++;
printf("%d", number);
Output:
11
The decrement operator -- decreases the value of an object by
one.
int number = 10;
number--;
printf("%d", number);
Output:
9
The ++ operator is used when you want to increase a variable by
one.
int count = 5;
count++;
printf("%d", count);
Output:
6
The -- operator decreases a variable by one.
int count = 5;
count--;
printf("%d", count);
Output:
4
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.
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
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.
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.
In prefix decrement, the -- operator is placed before the
variable.
int number = 10;
--number;
printf("%d", number);
Output:
9
In postfix decrement, the -- operator is placed after the
variable.
int number = 10;
number--;
printf("%d", number);
Output:
9
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.
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.
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.
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.
#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.
#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.
int i = 1;
while (i <= 5)
{
printf("%d ", i);
i++;
}
Output:
1 2 3 4 5
The variable is incremented inside the loop body.
int i = 5;
while (i >= 1)
{
printf("%d ", i);
i--;
}
Output:
5 4 3 2 1
int count = 0;
count++;
count++;
count++;
printf("Count = %d", count);
Output:
Count = 3
Increment is commonly used for counters.
int count = 5;
count--;
count--;
printf("Count = %d", count);
Output:
Count = 3
Decrement is useful when a counter needs to move downward.
#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.
#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.
int number = 10;
if (number > 5)
{
number++;
}
printf("%d", number);
Output:
11
The value is incremented only when the condition is true.
int number = 5;
printf("%d", ++number);
Output:
6
With prefix increment, the variable is incremented before its value is used by the 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.
int number = 5;
printf("%d", --number);
Output:
4
The variable is decremented before its value is used.
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.
++ or -- on expressions that are not
modifiable objects.#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
Practice the following programs:
++.--.Practice both prefix and postfix forms so that you understand when the variable is modified relative to the value produced by the expression.
++ increases a variable by one.-- decreases a variable by one.++variable.variable++.--variable.variable--.Question: Which operator is used to increase the value of a variable by one?