views:

214

answers:

4
int main()
{
   int i=3;
   (++i)++;        
   printf("%d",i);
}

This programs works with g++ compiler but not gcc. If i write i++++ or ++i++ it doesn't work in cpp also. I think there is difference between c-expression and c++-expression. Can somebody explain about L-value and R-value ?

+8  A: 

Edit: This answer is incorrect for the updated question, it applies to the question as originally stated.

(i++)++ shouldn't work with either gcc or g++, whether or not they are parsing the file as C or C++ in both languages postfix increment requires an lvalue as an operand and the result is an rvalue.

(Note that rvalue is only used formally in C++. In C the results of expressions are either explicitly lvalues or they're just not lvalues. The term rvalue isn't used in the normative text of the C standard at all.)

Simplisticly, an lvalue is an expression that refers to an object, i.e. conceptually a region of storage somewhere. A modifiable lvalue is something that it is valid to assign to so can appear on the left hand side of an assignment expression.

An rvalue is just a value, something that you can assign a modifiable lvalue from, but not something that you can assign to. It can only appear on the right side of an assignment expression.

g++ gives me the error: lvalue required as increment operand.

Prefix increment (and decrement) are slightly different. In C++ the result of a prefix increment is explicitly an lvalue (5.3.2/1 [expr.pre.incr]); in C it is explicitly not an lvalue (6.5.3.1 states that ++E is equivalent to (E+=1); 6.5.16 says that the result of any assignment is not an lvalue).

(++i)++ is, therefore, semantically correct only in C++, however it has undefined behaviour because you are storing a value to an object twice without an intervening sequence point.

Charles Bailey
@Charles Bailey:My guess is c-expression returns r-value and c++-expression returns l-value.
Jagan
@Jagan: Why do you think you should guess? It's just not true; in both C and C++ the result of postfix increment expression is an _rvalue_.
Charles Bailey
@Charles Bailey: What about preincrement expression ?
Jagan
@Jagan: The result of a prefix increment is an lvalue in C++ only, but your question doesn't (or didn't until your edit) mention prefix increment operators.
Charles Bailey
For user-defined types in C++, rvalues *can* appear on the left-hand side of an assignment, for example `std::string("hello") = std::string("world")`.
FredOverflow
@FredOverflow: Yes, there are discrepancies in C++ between the traditional use of _rvalue_ and what's actually true. In your example `op=` resolves to a member function call and you can call functions on rvalues of class type.
Charles Bailey
A: 

From wiki:

Some languages use the idea of lvalues and rvalues. Lvalues are values that have addresses being programmatically accessible to the running program (e.g., via some address-of–operator like "&" in C++), meaning that they are variables or dereferenced references to a certain memory location. Rvalues can be lvalues (see below.) or non-lvalues—a term only used to distinguish from lvalues. In C, the term lvalue originally meant something that could be assigned to (coming from left-value, indicating it was on the left side of the assignment operator), but since 'const' was added to the language, this now is termed a 'modifiable lvalue'.

Praveen S
A: 

I am getting the same error. lvalue required as increment operan. Could you specify if you are using any special flags?

MIkhail
A: 

$5.2.6-

The value obtained by applying a postfix ++ is the value that the operand had before applying the operator. [Note: the value obtained is a copy of the original value ] The operand shall be a modifiable lvalue. The type of the operand shall be an arithmetic type or a pointer to a complete object type. After the result is noted, the value of the object is modified by adding 1 to it, unless the object is of type bool, in which case it is set to true. [Note: this use is deprecated, see annex D. ] The result is an rvalue. The type of the result is the cv-unqualified version of the type of the operand. See also 5.7 and 5.17.

$5.3.2/1-

"The operand of prefix ++ is modified by adding 1, or set to true if it is bool (this use is deprecated). The operand shall be a modifiable lvalue."

That should explain the error you are getting for C++.

Chubsdad