0

Possible Duplicate:
(C) What is the difference between ++i and i++

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”);
       }
Community
  • 1
  • 1
  • 3
    Please use the search tool next time... – Oliver Charlesworth Apr 16 '11 at 11:58
  • The duplicate question has the answer to the question in the title. But the sample code you provided shows the increment operators used in a `for` loop. In that case, the distinction is irrelevant. The canonical style is `i++`, but the only important thing is that you pick one and be consistent with its use. (More detailed info is [here](http://stackoverflow.com/questions/4706199/post-increment-and-pre-increment-in-for-loop).) – Cody Gray - on strike Apr 16 '11 at 12:01
  • @cody can you explain me the code and how exactly gonna work ! –  Apr 16 '11 at 12:03
  • You have two nested loops (meaning one inside of the other), and a two-dimensional array. The loops simply loop through each dimension of the array. Easiest way to find out what will happen is to compile it and see! The point was simply that the choice between `i++` and `++i` **in a `for` loop** doesn't matter. – Cody Gray - on strike Apr 16 '11 at 12:05
  • thanks dude and can you recommend me a good book to improve my c programming skills ! –  Apr 16 '11 at 12:07
  • Duplicate: http://stackoverflow.com/questions/484462/difference-between-i-and-i-in-a-loop – KevenK Apr 16 '11 at 11:57

6 Answers6

2

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

phimuemue
  • 34,669
  • 9
  • 84
  • 115
1
++i Increments $i by one, then returns $i.
i++ Returns $i, then increments $i by one.
Dejan Marjanović
  • 19,244
  • 7
  • 52
  • 66
1

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.

Nanda
  • 604
  • 8
  • 20
0

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

Conner
  • 30,144
  • 8
  • 52
  • 73
BlackBear
  • 22,411
  • 10
  • 48
  • 86
  • 2
    Not very helpful if someone doesn't know what "pre-increment" and "post-increment" *mean*. Chances are, if they're asking this question, they don't have a good understanding of the difference. – Cody Gray - on strike Apr 16 '11 at 12:03
0
int i = 2;

int a = ++i; // a is 3, i is 3
int b = i++; // b is 3, i is 4
cweinberger
  • 3,518
  • 1
  • 16
  • 31
-1

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

satnhak
  • 9,407
  • 5
  • 63
  • 81
  • Erm, can you explain what the difference is? In the declaration of a for loop, on an int `i++` and `++i` are absolutely identical. – satnhak Apr 16 '11 at 12:00
  • http://stackoverflow.com/questions/4706199/post-increment-and-pre-increment-in-for-loop – satnhak Apr 16 '11 at 12:02