Issues with increment

Hello Everyone,
For some strange reason I cannot get the ++ (increment) to work properly with this statement.

while( digitalRead( MenuButton ) == HIGH ) {}
for (int i=0; i < 1e2; i++)
Serial.println(i, DEC);
ButtonPressed = ButtonPressed++;
Serial.println(ButtonPressed, DEC);
if ( ButtonPressed > 1)
{ ButtonPressed = 0; }

The part that does not increment is ButtonPressed = ButtonPressed++;

This part does. (int i=0; i < 1e2; i++)

I am using Arduino 1.5.8 version.

Comments please.

What output do you expect to see?

Probably [u]not[/u] what you meant...

ButtonPressed = ButtonPressed++;

Probably what you meant...

ButtonPressed++;

Why not simply use

ButtonPressed++;
for (int i=0; i < 1e2; i++)

Exactly which commands do you expect to execute with this for loop and what is 1e2 meant to be ?

  1. IIRC there are undefined side-effects with a statement like this:
ButtonPressed = ButtonPressed++;

The value of (ButtonPressed++) can be just ButtonPressed and then the increment is performed so that if ButtonPressed is initially zero, the statement will assign zero to ButtonPressed and then the increment will be performed. As Coding Badly and UKHeliBob have pointed out, ButtonPressed++; is a much safer way to do it. Or you can use ButtonPressed += 1; or ButtonPressed = ButtonPressed + 1;

  1. What's wrong with 100?

Pete

In this statement ButtonPressed = ButtonPressed++;

The value of ButtonPressed is evaluated (ready for the assignment), then ButtonPressed will be incremented (due to the ++) Finally, the assignment happens.

The nett result is that ButtonPressed will come out of it all with the same value that it started with.

The nett result is that ButtonPressed will come out of it all with the same value that it started with.

Try this

void setup() 
{
  Serial.begin(115200);
  int test = 0;
  test = test++;
  Serial.println(test);
  test = test++;
  Serial.println(test);
}

void loop() 
{
}

I get 1 2 as the output

UKHeliBob: Try this

void setup() 
{
  Serial.begin(115200);
  int test = 0;
  test = test++;
  Serial.println(test);
  test = test++;
  Serial.println(test);
}

void loop() { }



I get
1
2
as the output

I take your word for it as I don't have any hardware to test. Looks like I was wrong. So it appears the assignment is happening BEFORE the ++, thus giving the ++ the opportunity to still work after the assignment has happened.

Which means that the OPs original code should work OK.

I repeat, this to the OP What output do you expect to see?

Probably sleep time in their zone :sleeping:

UKHeliBob: Try this ... ... I get 1 2 as the output

Tried it ... worked a few times. Tried other code, then tried this again ... 0, 0 as output. Erased Due, cycled USB power, restarted IDE, upload ... still 0, 0 as output.

Other methods work fine.

The expression

i = i++;

has Undefined Behaviour in C & C++. The reason for this is the concept of Sequence Points, the location of sequence points (in this clause there is only one, at the end of the evaluation of the entire expression), and clause 6.5#2 of the C99 spec, which states "Between the previous and next sequence point an object shall have its stored value modified at most once by the evaluation of an expression. Furthermore, the prior value shall be accessed only to determine the value to be stored.".

This expression tries to assign a value to i twice within a single sequence point, so all bets are off as to the behaviour.

crimony is correct.

Your assignment has undefined results.

http://www.gammon.com.au/forum/?id=12153#trap8

A note about "undefined behavior":

http://en.wikipedia.org/wiki/Undefined_behavior

When an instance of undefined behavior occurs, so far as the language specification is concerned anything could happen, maybe nothing at all.

  for (int i=0; i < 1e2; i++)
  1. What’s wrong with 100?

Agreed. Why not write:

   if ( ButtonPressed > 1e0)

Putting exponents on small numbers just looks silly.

Good grief!!

I read:

   if ( ButtonPressed > 1e0)

as: "If ButtonPressed greater than el-e-zero"! But then, it's 12:25AM here.

Leo the lion?

Leo the lion?

Yep...I think that was him!

Thank you everyone. I did not have this problem with the Chipkit Uno MPIDE. Only encountered it with Arduino. I'll have to change things around a bit. As to the 1e2 . The program had larger numbers i.e. 10e3, 20e6, so I just kept the same format. Again Thanks.