pYro's puzzle #1: Increment this.

Hi all,

Over the years I've encountered some great topics regarding C++ that I found interesting enough to archive in my 'box o stuff'. As I have nothing better to do on a Wednesday night, I'll convey this information via interpretive dance in puzzle form for anybody else looking for a time passer.If you know the answer hold off a little ( or for a few posts between Q & A ) for people to have a crack. If people like these sorts of things, I'll post more.

And before anyone asks, I'm not trying to get someone to do my homework.

#1 Increment This

For the first entry, here is my own extension to a common puzzle. This requires a short preface.

Take the code below, what is printed to the serial monitor? ( not the question )

int i = 0;
int j = 1;

Serial.println( i+++j, DEC );

For those of you who haven't seen this, it is a classic example of 'order of evaluation', without looking at associativity, operator precedence defines how this is evaluated.

| Precedence | | Operator | | - | - | - | | 1 | | ++ ( Suffix increment ) | | 2 | | ++ ( Prefix increment ) | | 2 | | + ( Unary plus ) |

So... The suffix operator has precedence, and the code is:

i++ + j; //( i Suffix increment ) + j

rather than:

i + ++j; //i + ( j prefix increment )

As you can see operator precedence explains why this works... Or does it.

Why does the code below fail to compile while the two equivalent versions beneath it compile fine?

i++ + ++j;
i+++ ++j;

Hint #1: the answer is not related to the spaces in the code, if you believe this is the answer, explain why the spaces didn't cause error with the version: 'i+++j'

Hint #2



Anyone had a go yet?

I'll bump it back up for others to see anyway. This may be difficult, but I thought it was a good one for a first, the next one I have planned should be a little easier.

I'll play along.

Hint #2 fails to compile because of the way C combines characters into diagraphs to create a lexical unit and operator precedence. The compiler sees this...


( ( i ++ ) ++ ) j ;

From the perspective of the outer operator (the second ++), the expression is...

( ( i + 1 ) ++ ) j ;

...which fails to compile because the increment operator requires a variable (an "lvalue") as the operand.

The five pluses is just an extension of the above...

i+++++j; seen as...

i ++ ++ + j ;
( ( i ++ ) ++ ) + j ;

Yup, you got it, nice answer too.

C or C++ hasn't even come into play when this occurs, this happens even before macros are expanded. The tokenizer creates the largest possible token from the source input, so the token '++' is considered before '+'.

pYro_65: I'll convey this information via interpretive dance in puzzle form ...

I'd prefer interpretive dance. A YouTube video will be an acceptable medium.

Using this dance troop...