In this blog post, we’ll explore the difference between post-decrement and pre-decrement using a simple C code example. C programming offers several powerful and flexible tools for managing variables and performing operations on them. Among these tools are the increment and decrement operators, which allow you to modify a variable’s value quickly.
Table of Contents
ToggleWhat do i– and –i actually mean in C Programming?
–i is a decrement operator in C programming. It subtracts one from the value of i and returns the new value. For example, if i is 5, then –i will make i 4 and return 4 as well. This is different from i–, which also subtracts one from i, but returns the old value. For example, if i is 5, then i– will make i 4 but return 5 as well.
#include<stdio.h>
int main() {
int i;
i = 4; //Initial value of i
printf("%d\n" , i--); //using post decrement
printf("%d\n" , i);
printf("%d\n" , --i); //using pre-decrement
printf("%d\n" , i);
return 0;
}
Now have a look at Post-decrement and Pre-decrement
In the code example above, we have a variable i
initialized to the value 4. We then use both post-decrement and pre-decrement operators to modify the value of i
. Let’s go through the code step by step.
Post-decrement (i
–)
The line printf("%d\n", i--);
is an example of post-decrement. This means the value of i is first assigned and then decreased by 1. When you use i
–, it decrements the value of i
but returns the original value of i
before the decrement.
In our code, this means that printf
will print the value of i
(which is 4) before incrementing it. So, the output of this line will be 4
, and the value of i
will become 3.
Pre-decrement (--i
)
The line printf("%d\n", --i);
is an example of pre-decrement. This means the value of i is first decremented by 1 and then assigned to i. When you use --i
, it decrements the value of i
and returns the updated value.
In our code, this means that printf
will print the updated value of i
(which is now 3 because it was decremented in the previous step). So, the output of this line will be 2, and the value of i
will remain 2.
Output
Here’s the output of the code:
4
3
2
2
As you can see, using post-decrement (i--
) returns the original value and then decrements it, while pre-decrement (--i
) decrements the value first and then returns it.
Conclusion
Understanding the difference between post-decrement and pre-decrement is essential in C programming, as it can affect the behavior of your code. Post-decrement is useful when you need to use the original value of a variable before decrementing it, while pre-decrement is handy when you want to decrement the variable first and then use its updated value.
Remember to choose the decrement operator that best suits your programming needs to ensure your code behaves as expected.
Have a look at this YouTube video for more convenience.
Excited to see this applies to increment as well. Then check it out:: Clarity to 2 Concepts- Post-Increment and Pre-Increment in C (codershot.com)