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