12
#include <stdio.h>

int main()
{
    int a=8,b=9,c;
    c=a+++b;
    printf("%d%d%d\n",a,b,c);
    return 0;
}

The program above outputs a=9 b=9 and c=17. In a+++b why is the compiler takes a++ and then adds with b. Why is it not taking a + and ++b? Is there a specific name for this a+++b. Please help me to understand.

cHao
  • 84,970
  • 20
  • 145
  • 172
Angus
  • 12,133
  • 29
  • 96
  • 151
  • 2
    I'm sure there was an even more general question about this... with multiple + but it's very difficult to search for `++++`... – nico Aug 29 '11 at 17:11
  • 1
    I'm not sure why you've got the bit about sequence points in the question: "There should not be more than one modification of a variable before the sequence point.If it happens then the behaiour is undefined.". Which variable do you think is modified more than once before a sequence point here? – sepp2k Aug 29 '11 at 17:35

3 Answers3

32

I like the explanation from Expert C Programming:

The ANSI standard specifies a convention that has come to be known as the maximal munch strategy. Maximal munch says that if there's more than one possibility for the next token, the compiler will prefer to bite off the one involving the longest sequence of characters. So the example will be parsed

c = a++ + b;
Community
  • 1
  • 1
cnicutar
  • 178,505
  • 25
  • 365
  • 392
  • Never knew this, nice answer. – Matthew Blanchard Aug 29 '11 at 17:11
  • @cnicutar:Good explanantion.Thanks for making me to understand. – Angus Aug 29 '11 at 17:14
  • +1. This multiple '+'s thing makes for ugly code though. I would be much happier seeing c = (a++) + b – Jon Martin Aug 29 '11 at 17:15
  • @cnicutar: Does it hold good for C++ as well? – Alok Save Aug 29 '11 at 17:18
  • @Als I believe it does. For instance I remember g++ complaining about `vector>`. Then again, I don't know much C++. – cnicutar Aug 29 '11 at 17:19
  • The maximal much rule can cause some surprising results. `a+++++b` is tokenized as `a ++ ++ + b`, which is a syntax error. If not for the rule, it *could* be tokenized as `a ++ + ++ b`, which is legal and meaningful. The point of the rule is to guarantee that all compilers will do the same thing, and if you really want to write `a ++ + ++ b`, adding white space will make it unambiguous to the compiler *and* clear to any human readers. – Keith Thompson Nov 13 '12 at 20:41
5

Read Maximum Munch Principle

"maximal munch" or "longest match" is the principle that when creating some construct, as much of the available input as possible should be consumed.


Every compiler has a tokenizer, which is a component that parses a source file into distinct tokens (keywords, operators, identifiers etc.). One of the tokenizer's rules is called "maximal munch", which says that the tokenizer should keep reading characters from the source file until adding one more character causes the current token to stop making sense

Sadique
  • 22,572
  • 7
  • 65
  • 91
-1

Order of operations in C dictate that unary operations have higher precedence than binary operations.

You could use a + (++b) if you wanted b to be incremented first.

Jacob Tabak
  • 8,044
  • 3
  • 24
  • 32