What is the difference between pre-increment and post-increment in the cycle (for/while)?

孤街浪徒 提交于 2019-11-26 22:19:20
zennehoy

Since the statement i++ ends at the ; in your example, it makes no difference whether you use pre- or post-increment.

The difference arises when you utilize the result:

int j = i++; // i will contain i_old + 1, j will contain the i_old.

Vs:

int j = ++i; // i and j will both contain i_old + 1.
Tim

Depends on how you use them.

  • i++ makes a copy, increases i, and returns the copy (old value).
  • ++i increases i, and returns i.

In your example it is all about speed. ++i will be the faster than i++ since it doesn't make a copy.

However a compiler will probably optimize it away since you are not storing the returned value from the increment operator in your example, but this is only possible for fundamental types like a int.

Basic answer for understanding. The incrementation operator works like this:

// ++i
function pre_increment(i) {
    i += 1;
    return i;
}
// i++
function post_increment(i) {
    copy = i;
    i += 1;
    return copy;
}

A good compiler will automatically replace i++ with ++i when it detect that the returned value will not be used.

Pbk1303

In Pre-Increment the initial value is first incremented and then used inside the expression.

a=++i;

In this example suppose the value of variable i is 5 then value of variable a will be 6 because, the value of i gets modified before using it in a expression.

In Post-Increment value is first used in a expression and then incremented.

a=i++;

In this example suppose the value of variable i is 5 then value of variable a will be 5 because, value of i gets incremented only after assigning the value 5 to a .

Taha
#include <stdlib.h>
#include <stdio.h>

int main(int argc, char **argp)
{
    int x = 5;

    printf("x=%d\n", ++x);
    printf("x=%d\n", x++);
    printf("x=%d\n", x);

    return EXIT_SUCCESS;
}

Program Output:

x=6
x=6
x=7

In the first printf statement x is incremented before being passed to printf so the value 6 is output, in the second x is passed to printf (so 6 is output) and then incremented and the 3rd printf statement just shows that post increment following the previous statement by outputting x again which now has the value 7.

i++ uses i's value then increments it but ++i increments i's value before using it.

The difference between post- and pre-increment is really, in many cases subtle. post incremenet, aka num++, first creates a copy of num, returns it, and after that, increments it. Pre-increment, on the other hand, aka ++num, first evaluates, then returns the value. Most modern compilers, when seeing this in a loop, will generally optimize, mostly when post increment is used, and the returned initial value is not used. The most major difference between the 2 increments, where it is really common to make subtle bugs, is when declaring variables, with incremented values: Example below:

int num = 5;
int num2 = ++num; //Here, first num is incremented, 
                  //then made 6, and that value is stored in num2;

Another example:

int num = 5;
int num2 = num++; //Here, num is first returned, (unfortunately?), and then 
                  //incremented. This is useful for some cases.

The last thing here I want to say is BE CAREFUL WITH INCREMENTS. When declaring variables, make sure you use the right increment, or just write the whole thing out (num2 = num + 1, which doesn't always work, and is the equivalent of pre-increment). A lot of trouble will be saved, if you use the right increment.

it does not matter if you use pre or post increment in an independent statement, except for the pre-increment the effect is immediate

//an example will make it more clear:


int n=1;
printf("%d",n);
printf("%d",++n);// try changing it to n++(you'll get to know what's going on)

n++;
printf("%d",n);

output: 123

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!