Advertisement



< Prev
Next >



C - Decrement Operator










Pre-decrement operation


This operation is named pre-decrement because the decrement operator(--) comes before the operand.
Sequence of operation in pre-decrement operation is -
/* Decrement Operator Example in C */

#include<stdio.h>
int main()
{
char ch= 'g';
short sh= 200;
int i=50;
long l=999999;
float f=10.5f;
double d= 20.5;


printf("Original char : %c \n", ch);
printf("Incremented char : %c \n", --ch);

printf("Original short : %d \n", sh);
printf("Incremented short : %d \n", --sh);

printf("Original int : %d \n", i);
printf("Incremented int : %d \n", --i);

printf("Original float : %f \n",  f);
printf("Incremented float : %f \n", --f);

printf("Original double : %lf \n", d);
printf("Incremented double :%lf", --d);

return 0;
}


Output is


Original char : g
Incremented char : f
Original short : 200
Incremented short : 199
Original int : 50
Incremented int : 49
Original float : 10.500000
Incremented float : 9.500000
Original double : 20.500000
Incremented double :19.500000





Post-Decrement operator


/* Increment Operator Example in C */

#include
int main()
{
char ch= 'g';
short sh= 200;
int i=50;
long l=999999;
float f=10.5f;
double d= 20.5;


printf("Original char : %c \n", ch);
printf("Incremented char : %c \n", ch--);


printf("Original short : %d \n", sh);
printf("Incremented short : %d \n", sh--);


printf("Original int : %d \n", i);
printf("Incremented int : %d \n", i--);

printf("Original float : %f \n",  f);
printf("Incremented float : %f \n", f--);

printf("Original double : %lf \n", d);
printf("Incremented double :%lf", d--);

return 0;
}
Output -
Original char : g
Incremented char : g
Original short : 200
Incremented short : 200
Original int : 50
Incremented int : 50
Original float : 10.500000
Incremented float : 10.500000
Original double : 20.500000
Incremented double :20.500000


Program Analysis





Advertisement




Displaying decremented value after a post-decrement operation


/* Increment Operator Example in C */

#include
int main()
{
char ch= 'h';
short sh= 100;
int i=10;
long l=99999;
float f=20.5f;
double d= 15.5;


printf("Original char : %c \n", ch);
ch--;
printf("Incremented char : %c \n", ch);


printf("Original short : %d \n", sh);
sh--;
printf("Incremented short : %d \n", sh);


printf("Original int : %d \n", i);
i--;
printf("Incremented int : %d \n", i);


printf("Original float : %f \n",  f);
f--;
printf("Incremented float : %f \n", f);


printf("Original double : %lf \n", d);
d--;
printf("Incremented double :%lf", d);

return 0;
}


Output-


Original char : h
Incremented char : g
Original short : 100
Incremented short : 99
Original int : 10
Incremented int : 9
Original float : 20.500000
Incremented float : 19.500000
Original double : 15.500000
Incremented double :14.500000


Program Analysis







Post and pre-decrement operators in arithmetic expressions.


/* Post-increment and pre-increment operator in arithmetic expressions */

#include<stdio.h>
int main()
{
int a =10, b=10, c=10, d=10;

int result = a-- - 10; /* first value of a(10) is read and subctracted with 10 and then it is decremented to 9 */
printf("Result1 = %d \n", result);
printf("a = %d \n", a);

result = --b - 10;     /*first the value of a(9) is decremented to 8 and then it is subtracted by 10. */
printf("Result2 = %d \n", result);
printf("b = %d \n", b); 

result = --c + c--;
printf("Result3 = %d \n", result);
printf("c = %d \n", c);

result = d-- + 10 + --d;
printf("Result4 = %d \n", result);
printf("d = %d", d);

return 0;
}


Output-


Result1 = 0
a = 9
Result2 = -1
b = 9
Result3 = 17
c = 8
Result4 = 28
d = 8





Explaining the sequence of operations in our last code.



a-- - 10
  1. Due to post-decrement operator with a, its value 10 is first read to be subtracted by 10.
  2. Next, the original value of a is decremented to 9.
  3. Finally, result of 10-10 = 0, is passed to the result.



--b - 10
  1. Due to pre-decrement operator with b, its value 10 is first decremented to 9
  2. Next, this decremented value of b, 9 is subtracted by 10
  3. Finally, result of 9-10 = -1 , is passed to the result.




--c + c--
  1. Due to pre-decrement operator with c, first its value 10 is decremented to 9
  2. In the next expression, due to post-decrement operator with c, first the current value of c i.e. 9 is read and added to 9 from the previous expression and then c is decremented to 8
  3. Finally, the result of 9+9 = 18, is passed to the result.




d-- + 10 + --d
  1. Due to post-decrement operator with d, first its value is read to be added to 10, resulting 20 and then d is decremented to 9
  2. In the next expression, due to pre-decrement operator with d, first the current value of d 9 is first decremented to 8 and then it is added to 20 from the previous expression.
  3. Finally, the result of 20+8 = 28, is passed to the result.




Please share this article -




< Prev
Next >
< Increment Operator in C
Compound Assignment Operators in C >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement