0

Editing here to clarify. The headline asks the question but the example given was not ideal. This was a more general Q than about the example code.

Compilers have sequence points, places where you can be assured that the equations have been evaluated etc and no lingering effects will occur: the compilation is stable. When I couldn't test the original example I went looking for theory, and came across SE/SO posts where there was some discussion about issues like:

   int a = 1;
   int b = a++ - a++ + a++;

What would you expect the values of a and b to be after this? It turns out it hinges on whether the arithmetic operators are sequence points. Apparently this was not well settled in early versions of C, and in other languages (like Java) the results may differ yet again.

I now understand that an expression like (item++ > max) contains no sequence points -- the > operator does not trigger evaluation, but assignment operators and some arithmetic operators do.

//================================

Original Q:

I've read several Qs here concerning x++ vs ++x but I don't see this specific case. I'm still something of a C novice here.

Do these two code fragments do the same thing?

  // assume itemNum == maxItem on entry

if (++itemNum > maxItem) itemNum = 0;

if (itemNum++ > maxItem) itemNum = 0;

I gather that when the increment takes effect depends on the occurrence a 'sequence point' but I don't know if the comparison creates such an inflection.

closed as off-topic by Juraj, VE7JRO, sempaiscuba, gre_gor, MatsK Feb 8 at 18:33

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question does not appear to be about Arduino, within the scope defined in the help center." – Juraj, VE7JRO, sempaiscuba, gre_gor, MatsK
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 2
    why don't you just write a short sketch that prints out the value of itemNum and ++itemNum, then itemNum and itemNum++ – jsotola Feb 7 at 4:22
  • @jsotola - Didn't have any platform to test on. As I comment elsewhere, this is more a question about what constitutes a 'sequence point' - about when the ++ occurs than what ++ is. – Jim Mack Feb 7 at 15:35
2

Both ++x and x++ increment the value of x. The difference is whether you evaluate the value of x in your expression (formula) before or after incrementing it.

int x1 = 0;
int y1 = 0;

int x2 = x1++; //Post-increment: Move current value of x1 (0) to x2, THEN increment x1
int y2 = ++y1; //Pre-increment: Increment y1. Then move the NEW value of y1 (1) to y2.

In the above, x2 will be 0, and y2 will be 1.

The same logic applies to your if expression. If you use pre-increment, your if statement makes its decision on the new value after incrementing it.

If you use post-increment, the if statement makes its decision based on the value before it's incremented.

  • I do understand the concept of pre/post ++, it just wasn't / isn't clear to me what constitutes a 'sequence point' which forces the increment to occur. This apparently has changed with different versions of C. – Jim Mack Feb 7 at 15:32
  • I am not aware of any differences in the handling of pre and post increment/decrement operators in C. Where are you getting that information? – Duncan C Feb 7 at 15:49
  • There are several discussions here on the topic. Here's one: stackoverflow.com/questions/4865599/… – Jim Mack Feb 7 at 17:32
  • @JimMack But where is the description of how it's different in different versions of C? – Duncan C Feb 8 at 13:36
  • Look at the discussion under stackoverflow.com/questions/4445706 This is about C++, not C, but it's where I got the notion – Jim Mack Feb 8 at 17:20

Not the answer you're looking for? Browse other questions tagged or ask your own question.