Decrement Operator(–)
The operator –- subtracts 1 (one) from the operand. Decrement operators can be used in two forms.
(i) post decrement (i–)
(ii) Pre decrement (–i)
(i)Post decrement operator (i–)
int i=1,n; | ||
n=i–; | n=i; i=i-1; |
n=1; i=0; |
In post decrement the value of the variable is used first and then it is decremented.
#include<stdio.h> int main() { int a,i=10; a=i--; printf("a = %d i = %d\n",a,i); return(0); }
/* Output */ a = 10 i = 9
(ii)Pre decrement Operator (–i)
int i=1,n; | ||
n=–i; | i=i-1; n=i; |
i=0; n=0; |
In pre decrement the value of the variable is decremented first and then it is used.
#include<stdio.h> int main() { int a,i=10; a=--i; printf("a = %d i = %d\n",a,i); return(0); }
/* Output */ a = 9 i = 9
Note: i– or –i when used independently mean the same that is decrement the value by one.
if we have a=10;
a–;
–a;
a=a-1;
a-=1;
On execution of any of the above statements we will get
a=9;