Why pre-increment operator gives rvalue in C?

C doesn't have references. In C I returns a reference to I (lvalue) whereas in C it returns a copy(incremented).

C doesn't have references. In C++ ++i returns a reference to I (lvalue) whereas in C it returns a copy(incremented). C99 6.5.3.

1/2 The value of the operand of the pre? X ++ operator is incremented. The result is the new value of the operand after incrementation.

The expression ++Eis equivalent to (E+=1). ‘‘value of an expression’’ rvalue However for historical reasons I think "references not being part of C" could be a possible reason.

But still I can't understand why it returns a copy in pre-increment. Why just not return incremented I as it does in say, i=j. Here, original i, after being changed, is returned.

Similarly, in i=i+1, i, itself could be returned after being changed. – Happy Mittal Jan 26 '11 at 8:20 1 @HappyMittal: Expressions don't really return values, they have value (or evaluate to a value) and other properties such as type and lvalue-ness, the expression ++i doesn't "return" a copy, there is no copy. ++i evaluates to the new value of i.

There is no copy unless you use it in a larger expression that makes a copy. – Charles Bailey Jan 26 '11 at 8:28 @Charles Bailey : Ok, I agree that in ++i evaluates to new value of i. But my question is that in i=j, I is changed and expression yields an lvalue.So ++i i.e.

I=i+1 is pretty much same as i=j, then why in this case, expression doesn't yield an lvalue? – Happy Mittal Jan 26 '11 at 8:35 1 @Happy : "Ok, I agree that in ++i evaluates to new value of i" and rvalue means **value** of an expression (C99). I=j also evaluates to a value.

Try taking address of (i=j) and you'll surely get an error – Prasoon Saurav Jan 26 '11 at 8:37 sorry..I didn't know i=j also returns rvalue. Now understood. – Happy Mittal Jan 26 '11 at 8:43.

Off the top of my head, I can't imagine any useful statements that could result from using a pre-incremented variable as an lvalue. In C++, due to the existence of operator overloading, I can. Do you have a specific example of something that you're prevented from doing in C, due to this restriction?

2 This should be a comment. – Filip Ekberg Jan 26 '11 at 8:00 @Jason..yes. I can't do this in C - int *ptr = &++i – Happy Mittal Jan 26 '11 at 8:01 1 @Filip It's my (admittedly non-rigorous) answer to the question which was posed.

The addition of overloaded operators in C++ made the pre-increment-as-lvalue construct more valuable, hence it was added. – Jason LeBrun Jan 26 '11 at 8:24 +1, I think this is an answer, albeit an uncertain one. Most ways to use the result of ++i as an lvalue in C, would be undefined behavior for lack of sequence points.So it's no great loss for the syntax to forbid it, although myfunc(&(++i)) would be alright and sometimes useful.

Note also that in C, (++i, i) is equivalent to what ++i would be if it evaluated to an lvalue, so even where you have a use for it, it's still no huge loss that it isn't. – Steve Jessop Jan 26 '11 at 9:41.

Able) lvalue. It is perhaps better considered as representing an object ‘‘locator value’’. What is sometimes called ‘‘rvalue’’ is in this International Standard described as the ‘‘value of an expression’’.

Hope that explains why ++i in C, returns rvalue. As for C++, I would say it depends on the object being incremented. If the object's type is some user-defined type, then it may always return lvalue.

That means, you can always write i++++++++ or ++++++i if type of I is Index as defined here: Undefined Behavior and Sequence Points Reloaded.

If the object's type is not a user-defined type then pre-increment must be an lvalue, anyway. – Charles Bailey Jan 26 '11 at 8:03 I am asking about behaviour in C because in C++, it makes sense to return lvalue as incremented object is to be returned. – Happy Mittal Jan 26 '11 at 8:04 @Charles: hehe.. yeah.

Since the OP didn't mention the type of the object, I replied only one side of the question. – Nawaz Jan 26 '11 at 8:04 @Happy Mittal: see my answer again. I updated it!

– Nawaz Jan 26 '11 at 8:07 @Downvoter: please specify the reason! – Nawaz Jan 26 '11 at 8:10.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions