0

I tried to understand how the matter of increment variable by 1 before and after the operation and the behavior is not so clear to me.

int i =1;
printf("%d\n",(++i)*(++i)); --> 9
****************
printf("%d\n",(++i)*(++i)*(++i)); --> 36

Apparently it can be seen that the first operation performed the operation three squared. And the second operation performed the operation three squared times four. How does this situation make sense if I increase the value of i by one each time?

  • *How does this situation make sense if I increase the value of i by one each time?* It doesn't make sense, and you shouldn't write it. Really. These expressions really, really don't make sense, and you really shouldn't write them. If you want the square of `i`, write `i*i`. If you want the cube of `i`, write `i*i*i`. What do *you* think `(++i)*(++i)` should mean? Why would you ever write it? Why would you want to increment `i`'s value twice like that, in the middle of doing a multiplication? – Steve Summit Aug 05 '22 at 11:48
  • And, in fact, it doesn't even matter "Why you would ever write it?" or "What do *you* think it should mean?", because the C Standard itself has declared that these expressions don't make sense, and are *undefined*. (What, specifically, makes them undefined? How can you learn which expressions are undefined, and that you shouldn't write? You can learn all about that at the [linked question](https://stackoverflow.com/questions/949433).) – Steve Summit Aug 05 '22 at 11:54

0 Answers0