开发者

difference between c's expression and c++'s expression

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 ?


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.


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'.


$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++.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜