9

Possible Duplicate:
Why doesn't a+++++b work in C?
3 Plus between two variables in c

I tried searching for this but couldn't find any results.

A code with c=a+++++b fails to compile (gcc) whereas for c=a++ + ++b, it compiles successfully. c=a+++ ++b also works. c=a++ +++b fails.

Why is the whitespace making such a difference here? Or am i missing an important concept of C?

Community
  • 1
  • 1
nims
  • 3,751
  • 1
  • 23
  • 27
  • 8
    not really sure why this is receiving down votes as id say its a perfectly valid and well structured question. One which im afraid I don't know the answer to though. – Jon Taylor Jun 29 '12 at 12:32
  • 7
    I would like to know about this... why is it closed? – nullpotent Jun 29 '12 at 12:33
  • 2
    There are too many questions like this already on SO. Search for them – RedX Jun 29 '12 at 12:34
  • 14
    It's called the "maximum munch" rule. In the early stages of processing when the lexer is converting the sequence of characters into tokens the rule is that it must take as many characters as form a valid token into each token. `++` and `+` are both tokens so when the lexer comes across `+++` it takes the first two pluses as these form a valid token but must start a new token with the third plus as `+++` is not a valid part of any token. The lexer is relatively dumb and doesn't consider any possible syntax errors that the chosen token sequence might cause. – CB Bailey Jun 29 '12 at 12:37
  • @Romain I also though it this way, but why the whitespace making the difference, – nims Jun 29 '12 at 12:37
  • 2
    @nims: Because white space always splits tokens. – CB Bailey Jun 29 '12 at 12:38
  • http://symbolhound.com is good for searching for this type of thing. – Mat Jun 29 '12 at 12:40
  • 2
    @nims Yes, whitespace is important. Surely you know that `integer;` is not the same as `int eger;`! – Mr Lister Jun 29 '12 at 12:45
  • Be more creative - why to stop at 5 pluses ? Want more ? Here it is: `printf("%d\n", 1 + + - + + +- -+ + + + - - - - - + - - + - - -+ - - + - - - + - - 1);` – Agnius Vasiliauskas Jun 29 '12 at 13:03
  • @Mr Lister Unless there is macro `#define integer int eger` :-) – Agnius Vasiliauskas Jun 29 '12 at 13:18
  • @0x69 That's starting to sound like that unmentionable language now. – Mr Lister Jun 29 '12 at 13:23
  • You mean brainf* ? :-) To me question about 5 pluses also applies to brainf* :-) (Or at least such question has no real value at all) – Agnius Vasiliauskas Jun 29 '12 at 13:28

1 Answers1

6

Since ++ is a token, the parser interprets a+++++b the same as a ++ ++ + b, which is not the same as a ++ + ++ b!

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • OP said `a+++++b` fails to compile. – juergen d Jun 29 '12 at 12:37
  • That's right, yes, `a ++ ++ + b` doesn't compile, while `a ++ + ++ b` does. Because `a ++ ++ + b` has no valid meaning. Or as gcc sees it, `a++` is not an lvalue. – Mr Lister Jun 29 '12 at 12:40
  • exact duplicate http://stackoverflow.com/questions/5341202/why-doesnt-ab-work-in-c/5677289#5677289 – RedX Jun 29 '12 at 12:41