C Programming
In this tutorial we will learn about increment and decrement operators in C programming language.
Adding 1 and subtracting 1 from a variable is common and if we want to perform this task then we can write the following x = x + 1
and x = x - 1
.
In the following example we are increasing the value of x by 1.
//declare variable
int x;
//assign a value
x = 10;
//increase value by 1
x = x + 1;
The same result can be achived by using the increment operator.
We use the increment operator ++
to increase the value of a variable by 1.
In the following example we are increasing the value of x by 1.
//declare variable
int x;
//assign value
x = 10;
//increase value by 1
x++;
Note! x++
is equivalent to x = x + 1
in the above example.
Now, lets say we want to decrease the value of a variable x by 1. Then we can write the following code.
//declare variable
int x;
//assign value
x = 10;
//decrease value by 1
x = x - 1;
We can achieve the same result by using the decrement operator.
We use the decrement operator --
when we want to decrease the value of a variable by 1.
In the following example we are decreasing the value of x by 1.
//declare variable
int x;
//assign value
x = 10;
//decrease value by 1
x--;
Note! x--
is equivalent to x = x - 1
in the above code.
Both ++
and --
are unary operators.
Both x++
and ++x
means the same thing when they form statement independently.
Similarly, x--
and --x
means the same when they form statement independently.
But they behave differently when they are used in expression on the right hand side of an assignment statement.
Lets explore the difference.
In the following example we are using the increment operator ++
after the variable x on the right hand side of an assignment statement.
So, value of x will be used first then it will be increased by 1.
#include <stdio.h>
int main(void)
{
//declare variables
int x, y;
//assign value to x
x = 10;
printf("Before x++: Value of x = %d\n", x);
//assign value to y
y = x++ + 10;
printf("y = %d\n", y);
printf("After x++: Value of x = %d\n", x);
return 0;
}
Output
Before x++: Value of x = 10
y = 20
After x++: Value of x = 11
In the above code the value of x is first used in the assignment statement.
we have, y = x++ + 10;
so, we will first use the value of x i.e., 10 and then increase it by 1
y = 10 + 10;
y = 20;
and now increasing the value of x by 1
so, x = 11
In the following example we are using the increment operator ++
before the variable x on the right hand side of an assignment statement.
So, value of x will be first increased by 1 and then used.
#include <stdio.h>
int main(void)
{
//declare variables
int x, y;
//assign value to x
x = 10;
printf("Before ++x: Value of x = %d\n", x);
//assign value to y
y = ++x + 10;
printf("y = %d\n", y);
printf("After ++x: Value of x = %d\n", x);
return 0;
}
Output
Before ++x: Value of x = 10
y = 21
After ++x: Value of x = 11
In the above code the value of x is first increased and then used in the assignment statement.
y = ++x + 10;
since we have ++x so
x is now 10+1 = 11
now using the new value of x
so, y = ++x + 10
y = 11 + 10
y = 21
Similarly, we can have the two scenarios for the decrement operator.
In the following example we first use the value of x then decrease it by 1.
#include <stdio.h>
int main(void)
{
//declare variables
int x, y;
//assign value to x
x = 10;
printf("Before x--: Value of x = %d\n", x);
//assign value to y
y = x-- + 10;
printf("y = %d\n", y);
printf("After x--: Value of x = %d\n", x);
return 0;
}
Output
Before x--: Value of x = 10
y = 20
After x--: Value of x = 9
In the following example we first decrease the value of x then use it.
#include <stdio.h>
int main(void)
{
//declare variables
int x, y;
//assign value to x
x = 10;
printf("Before --x: Value of x = %d\n", x);
//assign value to y
y = --x + 10;
printf("y = %d\n", y);
printf("After --x: Value of x = %d\n", x);
return 0;
}
Output
Before --x: Value of x = 10
y = 19
After --x: Value of x = 9
ADVERTISEMENT