Why can't I increment a variable like this?

People can make mistakes when reading/writing code, around the ++x (pre-increment) or x++ (post increment) operators.

pre-increment:

int array[] = {0, 1, 2};
int i = 0;
printf("%d\n", array[++i]); // prints 1

post-increment:

int array[] = {0, 1, 2};
int i = 0;
printf("%d\n", array[i++]); // prints 0

By removing the possibility for that kind of confusion, the opportunity for the mistake is removed

17 Likes