What is the difference between ++i and i++?

What is the difference between ++i and i++?

Hey Miroslav,

This incremental and decremental can cause some confusion even if you are a beginner or an expert level, it’s basic, and we all tend to forget some of the basics but easily remember the working of ++i , i++ , --i , i--. Below, I have tried explaining to you the functionality of I hope this helped you clear up your doubts.

The ++i operator increments the value of i and then returns the incremented value. For example, if i is initially 1, ++i will increment i to 2 and return 2.

On the other hand, the i++ operator also increments the value of i, but it returns the original value of i before the increment. Using the same initial value of 1, i++ will increment i to 2 but return 1.

In a for loop, either ++i or i++ can be used, but ++i is more common, perhaps because it is used in the classic book “The C Programming Language” by Kernighan and Ritchie.

It is generally recommended to prefer ++i over i++ for clarity and consistency.

Regarding the efficiency of ++i and i++, there is typically no performance difference in modern compilers. Both operators are optimized to generate identical code. However, in C++, when used with objects, ++i and i++ can have different performance implications, as operator++() is a function call and may involve the creation of a temporary object to hold the intermediate value.

Happy coding !!!

Hey Macy,

Epic explanation to the ++i. --i . Just to boost your answer, I think examples would help @MiroslavRalevic understand better.

i++ is known as post-increment, while ++i is called pre-increment.

Post-increment (i++): Post-increment increments the value of i by 1 after the current operation is completed.

Example:

int i = 1, j; j = i++; // After this operation, j = 1 and i = 2

Pre-increment (++i): Pre-increment increments the value of i by 1 before the current operation.

Example:

int i = 1, j; j = ++i; // After this operation, j = 2 and i = 2

For a for loop, you can use either i++ or ++i; it doesn’t affect the number of iterations. Both will execute the loop the same number of times.

Example:

for (int i = 0; i < 5; i++)
    printf("%d ", i);

and

for (int i = 0; i < 5; ++i)
    printf("%d ", i);

Both loops will produce the same output: 0 1 2 3 4.

The choice between i++ and ++i only matters when the value of i is used in the same statement.

Example:

for (int i = 0; i < 5;)
    printf("%d ", ++i);

In this case, the output will be 1 2 3 4 5.

Hope I was able to add value to you explanation @macy-davis

Hey Miro,

Wow, the above two answers are really detailed and have examples; as I came across this, I was eager to share my insights as well.

In the context of incrementing a value, ++i increments the value and then returns it, while i++ returns the value and then increments it. The difference is subtle but can be important depending on the context.

When used in a for loop, it’s generally recommended to use ++i over i++. This is because ++i is slightly faster and more efficient. i++ can create an extra unnecessary copy of the value that is then discarded.

Example:

for (int i = 0; i < 5; ++i) printf("%d ", i);

In this loop, ++i is used, which increments i before its value is used in the loop. This is more efficient than using i++, especially when performance is critical.