Hacker News new | comments | show | ask | jobs | submit login

It's not about a parenthesis. It's about the compiler not understanding all valid possibilities.

x+ +3 works.

x++3 doesn't.

OK, I take it back. That's logical.

Technically speaking though, the compiler could see it as x+ +3, but it could equally see it as x ++3, which has two compiler errors: firstly, that's not actually a valid expression, and secondly (at least as I checked in the C99 standard, under section on postfix increment and decrement operators) the lvalue must be modifiable.

All I guess I'm saying here is that it could technically be a typo which leads to invalid syntax, or it could be technically correct, so most compilers will treat it as a syntax error.

Someone above said that gcc was being "closer to the standard", but the standard never attempts to cover this particular corner case. So the gcc devs have done something, to my mind, quite sensible. They treat it as a possible syntax error because it could either be:

  x+ +3;
  x++ 3;
  x ++3;
I'm more interested in how it handles x+++x; - I'd imagine that it's a syntax error. It could be:

  x++ + x;
  x + ++x;
  x+ + +x;
The C99 standard doesn't actually explain how to handle this. Perhaps they believe you shouldn't be so daft.

I'll stand by original statement that it's a syntax error and most good compilers will treat it as such, even if it is logical to treat x++3 as x+ +3.

Edit: scratch that ALL completely. I forgot about operator precedence.

If you take x++3, then suffix increment is a higher operator than suffix decrement and addition. In other words, the compiler will always try to evaluate the expression as

  x++ 3;
In other words, it's a syntax error. It can never evaluate to x + +3, because that's at a lower operator precedence.

Thus, it's a syntax error. Take that, downvoter of my original comment.

Edit x2: Definitive answer is here: https://news.ycombinator.com/item?id=5200523

Some days it just doesn't pay to get out of bed... :-)

Thanks for the pointer to my answer :-)

Similar text to that C++ text is in C99 at 6.4 [Lexical elements], paragraph 4. Paragraph 6 considers the example x+++++y (which is similar to your example), noting that it is to be parsed as x ++ ++ + y. (I guess it uses distinct variables x and y to avoid any distractions due to sequence point issues -- which are immaterial here as the thing fails to parse anyway.)

Sometimes I think that the C and C++ standards authors entered into the IOCCC before they wrote the examples :-)

> but it could equally see it as x ++3

What it could see is not a valid argument. A compiler could "see" all kinds of absolutely valid situations as invalid, but it doesn't.

Give me an example.

It could see

as two division operators and throw an error, but it considers it a comment (to be fair, comments are probably removed by a pre-processor).

No, it couldn't. That's a comment, and only a comment. It can't be anything else. Do you have any other examples?

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact