Search code examples
clvalue

Why doesn't a+++++b work?


int main ()
{
   int a = 5,b = 2;
   printf("%d",a+++++b);
   return 0;
}

This code gives the following error:

error: lvalue required as increment operand

But if I put spaces throughout a++ + and ++b, then it works fine.

int main ()
{
   int a = 5,b = 2;
   printf("%d",a++ + ++b);
   return 0;
}

What does the error mean in the first example?


Solution

  • printf("%d",a+++++b); is interpreted as (a++)++ + b according to the Maximal Munch Rule!.

    ++ (postfix) doesn't evaluate to an lvalue but it requires its operand to be an lvalue.

    ! 6.4/4 says the next preprocessing token is the longest sequence of characters that could constitute a preprocessing token"