开发者

What's exactly difference between i++ and ++i in C? [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

(C) What is the difference between ++i and i++ 开发者_C百科

int a[3][4]={1,2,3,4,5,6,7,8,9,10,11,12};



    for(i=0;i<3;i++)
{ 
          for(j=0;j<4;j++)
           {
            printf(“%-4d”,a[i][j]);
           }
           printf(“\n”);
       }


In the above case, you won't recognize it.

But it's essentially the following:

int x = a[i++] first reads from à afterwards, increases i

int x = a[++i] first increases i, afterwards reads from a


++i Increments $i by one, then returns $i.
i++ Returns $i, then increments $i by one.


i++ is a post-increment operator ie., the current value of i is used for the operation and the value is incremented by 1 after the operation.

++i is pr-increment operator ie., the value of i is incremented and the new value of is used in the operation.


int i = 2;

int a = ++i; // a is 3, i is 3
int b = i++; // b is 3, i is 4


++i is a pre-increment, while i++ is a post-increment.


In that situation, because the type is an int and it happens in a for-loop, nothing; there is no performance benefit to either.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜