Go Down

Topic: pYro's puzzle #1: Increment this. (Read 572 times) previous topic - next topic

pYro_65

Jan 09, 2013, 02:40 pm Last Edit: Nov 12, 2013, 08:28 pm by pYro_65 Reason: 1
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 )

Code: [Select]
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.

http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B#Operator_precedence




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


So...
The suffix operator has precedence, and the code is:
Code: [Select]
i++ + j; //( i Suffix increment ) + j

rather than:

Code: [Select]
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?
Code: [Select]
i+++++j;
Code: [Select]
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
Code: [Select]
i++++j;



Cheers.

pYro_65

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.

Coding Badly

#2
Jan 10, 2013, 08:14 am Last Edit: Jan 10, 2013, 08:16 am by Coding Badly Reason: 1
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...

Code: [Select]
i++++j;

...as...

Code: [Select]
( ( i ++ ) ++ ) j ;

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

Code: [Select]
( ( 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...

Code: [Select]
i+++++j;

...is seen as...

Code: [Select]
i ++ ++ + j ;
Code: [Select]
( ( i ++ ) ++ ) + j ;

pYro_65

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 '+'.

Nick Gammon


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


I'd prefer interpretive dance. A YouTube video will be an acceptable medium.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Coding Badly


Using this dance troop... http://www.military.com/video/forces/navy/gangnam-style-usna-spirit-spot/1845842268001/

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy