C Tutorial/Operator/Increment decrement Operator
Increment operator
Prefix: the value is incremented/decremented first and then applied.
Postfix: the value is applied and the value is incremented/decremented.
<source lang="cpp">#include<stdio.h>
main( )
{
int i = 3,j = 4,k; k = i++ + --j; printf("i = %d, j = %d, k = %d",i,j,k);
}</source>
i = 4, j = 3, k = 6
The decrement operator: --
<source lang="cpp">#include <stdio.h>
- include <stdlib.h>
int main() {
char weight[4]; int w; printf("Enter your weight:"); gets(weight); w=atoi(weight); printf("Here is what you weigh now: %i\n",w); w--; printf("w++: %i\n",w); w--; printf("w++: %i\n",w); return(0);
}</source>
Enter your weight:123 Here is what you weigh now: 123 w++: 122 w++: 121