-4
   #include <stdio.h>

   int main()
    {
     int c=10,b;
     b=++c+++c;
     printf("%d",b);
     return 0;
    }

Could someone please let me know,why it is throwing compilation error?

  • 2
    Ever heard of spaces? – Deanie Apr 14 '15 at 16:33
  • 2
    This code has a lot of problems but the issue you seem to want to know about is the [maximal munch rule](http://stackoverflow.com/q/5341202/1708801) – Shafik Yaghmour Apr 14 '15 at 16:33
  • -1: are you compiling in C or C++? They are different languages. -1: What is the compilation error? Saying there is an error is less useful than saying the error. -1: Asking about `+++` as a general rule without sufficient research effort shown. +1: actual reproducible example. Verdict: -2. Feel free to improve your question and @Yakk me and I'll change my vote. – Yakk - Adam Nevraumont Apr 14 '15 at 16:33

2 Answers2

3

The gibberish is tokenised as

++ c ++ + c

and parsed as

((++c)++) + c

This tries to increment the rvalue yielded by ++c, which isn't allowed. You can only increment an lvalue (or a class type, in C++).

Even if it were allowed, this would give undefined behaviour: you'd have an unsequenced modification and use of the value of c.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

The compilation error would be solved if you put a space in ++c + ++c

b = ++c + ++c;

But that just fixes the compilation error. What you are doing is Undefined Behavior. Trying to change the value of c more than once in the same expression using ++ will lead to undefined Behavior.

Read Why are these constructs (using ++) undefined behavior?

Community
  • 1
  • 1
Arun A S
  • 6,421
  • 4
  • 29
  • 43
  • 3
    Except this is undefined behaviour. For example, I get 23 with Clang and 24 with GCC when compiling as C++, along with a warning by both. – chris Apr 14 '15 at 16:35
  • @chris , Yes, it is Undefined Behavior. Never said it wasn't. – Arun A S Apr 14 '15 at 16:36