The increment operator (++) and the decrement operator (--) will increment or decrement the value stored in the integer variable by one.
int number = 6;
You can increment it by 1 with the following statement:
++number; // Increase the value by 1
After executing this statement, number will contain the value 7.
Similarly, you could decrease the value of number by 1 with the following statement:
--number; // Decrease the value by 1
These increment and decrement operators are both unary operators, which means that they're used with only one operand.
They increase or decrease an int value by 1.
If you write count++ in an expression, the incrementing of count occurs after its value has been used.
Imagine you have operation as below
num = num + 1;
you can simply this syntax use ++
.
num++;
using the same approach for this case
num = num - 1;
you can use -- syntax.
num--;
#include <stdio.h>
int main(void)
{ //w w w . ja v a 2s.com
int ultra = 0, super = 0;
while (super < 5)
{
super++;
++ultra;
printf("super = %d, ultra = %d \n", super, ultra);
}
return 0;
}
The code above generates the following result.
Now how to implement them in code. Let's write this code.
#include <stdio.h>
/*w w w . j a v a 2 s . c o m*/
int main() {
int a = 10;
a++;
printf("%d \n",a);
a++;
printf("%d \n",a);
++a;
printf("%d \n",a);
a--;
printf("%d \n",a);
a--;
printf("%d \n",a);
--a;
printf("%d \n",a);
return 0;
}
The code above generates the following result.
You can place the increment operator to the right of a variable, as shown next.
x++;
This expression tells C to use the current value of variable x and increment it by 1.
The variable's original value was 0 and 1 was added to 0, which resulted in 1.
The other way to use the increment operator is to place it in front or to the left of your variable, as demonstrated next.
++x;
Changing the increment operator's placement (postfix versus prefix) with respect to the variable produces different results when evaluated.
When the increment operator is placed to the left of the variable, it will increment the variable's contents by 1 first, before it's used in another expression.
#include <stdio.h>
int main()
{
int x = 0;
int y = 0;
printf("\nThe value of y is %d\n", y++);
printf("\nThe value of x is %d\n", ++x);
return 0;
}
In the first printf() function above, C processed the printf()'s output first and then incremented the variable y.
In the second statement, C increments the x variable first and then processes the printf() function, thus revealing the variable's new value.
The code above generates the following result.
The following program demonstrates increment operator placement further.
#include <stdio.h>
int main() //w ww .j a va 2s . c o m
{
int x = 0;
int y = 1;
x = y++ * 2; //increments x after the assignment
printf("\nThe value of x is: %d\n", x);
x = 0;
y = 1;
x = ++y * 2; //increments x before the assignment
printf("The value of x is: %d\n", x);
return 0;
} //end main function
The program above will produce the following output.
The -- operator is similar to the increment operator (++), but instead of incrementing number- based variables, it decrements them by 1.
The decrement operator can be placed on both sides (prefix and postfix) of the variable, as demonstrated next.
--x; x--;
The next block of code uses the decrement operator in two ways to demonstrate how number- based variables can be decremented by 1.
#include <stdio.h>
main() //from w ww . j a va 2 s . c o m
{
int x = 1;
int y = 1;
x = y-- * 4;
printf("\nThe value of x is %d\n", x);
y = 1; //reset variable value for demonstration purposes
x = --y * 4;
printf("\nThe value of x is now %d\n", x);
}
The code above generates the following result.