Post-increment and Pre-Increment
Rate this post

In this blog post, we’ll explore the difference between post-increment and pre-increment 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.

What does i++ and ++i actually mean in C Programming?

++i is an increment operator in C programming. It adds one to the value of i and returns the new value. For example, if i is 5, then ++i will make i 6 and return 6 as well. This is different from i++, which also adds one to i, but returns the old value. For example, if i is 5, then i++ will make i 6 but return 5 as well.

#include<stdio.h>
int main() {
     int i;
     i = 4; //Initial value of i
     
    printf("%d\n" , i++);  //using post increment
    printf("%d\n" , i);
     
   printf("%d\n" , ++i); //using pre-increment
   printf("%d\n" , i);

   return 0;
   }
     

Post-increment and Pre-increment

In the code example above, we have a variable i initialized to the value 4. We then use both post-increment and pre-increment operators to modify the value of i. Let’s go through the code step by step.

Post-increment (i++)

The line printf("%d\n", i++); is an example of post-increment. This means the value of i is first assigned and then incremented by 1. When you use i++, it increments the value of i but returns the original value of i before the increment.

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 5.

Pre-increment (++i)

The line printf("%d\n", ++i); is an example of pre-increment. This means the value of i is first incremented by 1 and then assigned to i. When you use ++i, it increments 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 6 because it was incremented in the previous step). So, the output of this line will be 6, and the value of i will remain 6.

Output

Here’s the output of the code:

4
5
6
6

As you can see, using post-increment (i++) returns the original value and then increments it, while pre-increment (++i) increments the value first and then returns it.

Conclusion

Understanding the difference between post-increment and pre-increment is essential in C programming, as it can affect the behavior of your code. Post-increment is useful when you need to use the original value of a variable before incrementing it, while pre-increment is handy when you want to increment the variable first and then use its updated value.

Remember to choose the increment 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 make a ceiling fan on/off function? Then check it out:: Ceiling Fan ON/OFF function in Html/CSS and javascript – CoderShot

0 0 votes
Course Rating

Share:

More Posts

Send Us A Message

0
Would love your thoughts, please comment.x
()
x