Why does c = ++(a+b) give compilation error?

CIncrement

C Problem Overview


After researching, I read that the increment operator requires the operand to have a modifiable data object: https://en.wikipedia.org/wiki/Increment_and_decrement_operators.

From this I guess that it gives compilation error because (a+b) is a temporary integer and so is not modifiable.

Is this understanding correct? This was my first time trying to research a problem so if there was something I should have looked for please advise.

C Solutions


Solution 1 - C

It's just a rule, that's all, and is possibly there to (1) make it easier to write C compilers and (2) nobody has convinced the C standards committee to relax it.

Informally speaking you can only write ++foo if foo can appear on the left hand side of an assignment expression like foo = bar. Since you can't write a + b = bar, you can't write ++(a + b) either.

There's no real reason why a + b couldn't yield a temporary on which ++ can operate, and the result of that is the value of the expression ++(a + b).

Solution 2 - C

The C11 standard states in section 6.5.3.1

> The operand of the prefix increment or decrement operator shall have > atomic, qualified, or unqualified real or pointer type, and shall be a > modifiable lvalue

And "modifiable lvalue" is described in section 6.3.2.1 subsection 1

> An lvalue is an expression (with an object type other than void) that > potentially designates an object; if an lvalue does not designate an > object when it is evaluated, the behavior is undefined. When an > object is said to have a particular type, the type is > specified by the lvalue used to designate the object. A modifiable > lvalue is an lvalue that does not have array type, does not have > an incomplete type, does not have a const-qualified type, and > if it is a structure or union, does not have any member > (including, recursively, any member or element of all contained > aggregates or unions) with a const-qualified type.

So (a+b) is not a modifiable lvalue and is therefore not eligible for the prefix increment operator.

Solution 3 - C

You are correct. the ++ tries to assign the new value to the original variable. So ++a will take the value of a, adds 1 to it and then assign it back to a. Since, as you said, (a+b) is a temp value, and not a variable with assigned memory address the assignment can't be performed.

Solution 4 - C

I think you mostly answered your own question. I might make a small change to your phrasing and replace "temporary variable" with "rvalue" as C.Gibbons mentioned.

The terms variable, argument, temporary variable and so on will become more clear as you learn about C's memory model (this looks like a nice overview: https://www.geeksforgeeks.org/memory-layout-of-c-program/ ).

The term "rvalue" may seem opaque when you're just starting out, so I hope the following helps with developing an intuition about it.

Lvalue/rvalue are talking about the different sides of an equals sign (assignment operator): lvalue = left hand side (lowercase L, not a "one") rvalue = right hand side

Learning a little about how C uses memory (and registers) will be helpful for seeing why the distinction is important. In broad brush strokes, the compiler creates a list of machine language instructions that compute the result of an expression (the rvalue) and then puts that result somewhere (the lvalue). Imagine a compiler dealing with the following code fragment:

x = y * 3

In assembly pseudocode it might look something like this toy example:

load register A with the value at memory address y
load register B with a value of 3
multiply register A and B, saving the result in A
write register A to memory address x

The ++ operator (and its -- counterpart) need a "somewhere" to modify, essentially anything that can work as an lvalue.

Understanding the C memory model will be helpful because you'll get a better idea in your head about how arguments get passed to functions and (eventually) how to work with dynamic memory allocation, like the malloc() function. For similar reasons you might study some simple assembly programming at some point to get a better idea of what the compiler is doing. Also if you're using gcc, the -S option "Stop after the stage of compilation proper; do not assemble." can be interesting (though I'd recommend trying it on a small code fragment).

Just as an aside: The ++ instruction has been around since 1969 (though it started in C's predecessor, B):
>(Ken Thompson's) observation (was) that the translation of ++x was smaller than that of x=x+1."

Following that wikipedia reference will take you to an interesting writeup by Dennis Ritchie (the "R" in "K&R C") on the history of the C language, linked here for convenience: http://www.bell-labs.com/usr/dmr/www/chist.html where you can search for "++".

Solution 5 - C

The reason is that the standard requires the operand being an lvalue. The expression (a+b) is not a lvalue, so applying the increment operator isn't allowed.

Now, one might say "OK, that's indeed the reason, but there is actually no real reason other than that", but unluckily the particular wording of how the operator works factually does require that to be the case.

> The expression ++E is equivalent to (E+=1).

Obviously, you cannot write E += 1 if E isn't a lvalue. Which is a shame because one could just as well have said: "increments E by one" and be done. In that case, applying the operator on a non-lvalue would (in principle) be perfectly possible, at the expense of making the compiler slightly more complex.

Now, the definition could trivially be reworded (I think it isn't even originally C but an heirloom of B), but doing so would fundamentally change the language to something that's no longer compatible with its former versions. Since the possible benefit is rather small but the possible implications are huge, that never happened and probably is never going to happen.

If you consider C++ in addition to C (question is tagged C, but there was discussion about operator overloads), the story becomes even more complicated. In C, it's hard to imagine that this could be the case, but in C++ the result of (a+b) could very well be something that you cannot increment at all, or incrementing could have very considerable side effects (not just adding 1). The compiler must be able to cope with that, and diagnose problematic cases as they occur. On a lvalue, that's still kinda trivial to check. Not so for any kind of haphazard expression inside a parenthesis that you throw at the poor thing.
This isn't a real reason why it couldn't be done, but it sure lends as an explanation why the people who implemented this are not precisely ecstatic to add such a feature which promises very little benefit to very few people.

Solution 6 - C

(a+b) evaluates to an rvalue, which cannot be incremented.

Solution 7 - C

++ tries to give the value to the original variable and since (a+b) is a temp value it cannot perform the operation. And they are basically rules of the C programming conventions to make the programming easy. That's it.

Solution 8 - C

When ++(a+b) expression performed, then for example :

int a, b;
a = 10;
b = 20;
/* NOTE :
 //step 1: expression need to solve first to perform ++ operation over operand
   ++ ( exp );
// in your case 
   ++ ( 10 + 20 );
// step 2: result of that inc by one 
   ++ ( 30 );
// here, you're applying ++ operator over constant value and it's invalid use of ++ operator 
*/
++(a+b);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondngView Question on Stackoverflow
Solution 1 - CBathshebaView Answer on Stackoverflow
Solution 2 - CChristian GibbonsView Answer on Stackoverflow
Solution 3 - CRoee GavirelView Answer on Stackoverflow
Solution 4 - CjgreveView Answer on Stackoverflow
Solution 5 - CDamonView Answer on Stackoverflow
Solution 6 - CCasper B. HansenView Answer on Stackoverflow
Solution 7 - CBabu ChandermaniView Answer on Stackoverflow
Solution 8 - CJeet ParikhView Answer on Stackoverflow