Pages: [1]   Go Down
Author Topic: pYro's puzzle #1: Increment this.  (Read 486 times)
0 Members and 1 Guest are viewing this topic.
North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 64
Posts: 2101
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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:
i++ + j; //( i Suffix increment ) + j

rather than:

Code:
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:
i+++++j;
Code:
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:
i++++j;


Cheers.
« Last Edit: November 12, 2013, 02:28:54 pm by pYro_65 » Logged


North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 64
Posts: 2101
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 197
Posts: 12741
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
i++++j;

...as...

Code:
( ( i ++ ) ++ ) j ;

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

Code:
( ( 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:
i+++++j;

...is seen as...

Code:
i ++ ++ + j ;
Code:
( ( i ++ ) ++ ) + j ;
« Last Edit: January 10, 2013, 02:16:34 am by Coding Badly » Logged

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 64
Posts: 2101
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

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

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 197
Posts: 12741
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset


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

Pages: [1]   Go Up
Jump to: