#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?
#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?
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
.
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?