473,289 Members | 1,743 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,289 software developers and data experts.

postfix vs prefix operator

Consider

int i = 10;

Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ?

I thought both these expressions yield only values.

I am unable to understand the difference

Kindly explain.

Thanks
V.Subramanian

Jun 23 '07 #1
8 7801
su**************@yahoo.com wrote:
Consider

int i = 10;

Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ?
Because they do. Why do we say that water is wet?
I thought both these expressions yield only values.
That would be incorrect. There are no such thing as "only values".
I am unable to understand the difference
You can take an address of an lvalue, you can initialise a reference
to-non-const with it, you can apply another increment to it...

int i = 10;
int *pi = &(++i);// OK
int &ri = ++i; // OK
++i--; // OK

You can't do those things with an r-value:

int i = 10;
int *pi = &(i++);// NOT OK
int &ri = i++; // NOT OK
i++--; // NOT OK

What book are you reading that doesn't explain that?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jun 23 '07 #2
In article <11*********************@a26g2000pre.googlegroups. com>,
su**************@yahoo.com says...
Consider

int i = 10;

Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ?

I thought both these expressions yield only values.

I am unable to understand the difference
From the looks of it, when you say "only values", you basically mean
"rvalues".

An rvalue is only a value -- something like zero. It doesn't necessarily
have any storage associated with it.

An lvalue is a variable or something along that general line -- a piece
of storage that holds some value at any given time.

As the difference between pre- and post-increment, the reason for the
difference is fairly simple. In the case of pre-increment, you increment
the variable, and the result IS that variable. In the case of post-
increment, we increment the variable, but the result is the value the
variable held _before_ the increment -- so the result canNOT be held in
that variable. This means the result _has_ to be the value independent
of the variable -- so we no longer have the address of a variable, and
instead have only the value itself.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 23 '07 #3
On Jun 24, 12:06 am, Jerry Coffin <jcof...@taeus.comwrote:

Thanks Jerry Coffin.

Kindly clarify the following doubt also related to the Lvalue.

Consider the following code:

int main()
{
int x = 100;

++x = 1000;

return 0;
}

If I create a C source file containing this code and compile it under
both VC++2005 express edition and also gcc under Linux, I am getting
compilation error for the assignment:
++x = 1000;
error being invalid Lvalue in assignment.

However if I create a C++ source file containing this code and compile
it under VC++ 2005 and also g++, I do not get any compilation error.

Questions:
----------
1) Does it mean that prefix operator in C does not produce Lvalue in C
unlike C++ ?

2) I read that, in C++ postfix increment and decrement operator have
higher precedence than the prefix operator. But in C they have the
same precedence. Am I right or wrong ?

Kindly explain.

Jun 24 '07 #4
In article <11*********************@g37g2000prf.googlegroups. com>,
su**************@yahoo.com says...

[ ... ]
1) Does it mean that prefix operator in C does not produce Lvalue in C
unlike C++ ?
That's correct -- in C 6.5.16/3 specifies that: " An assignment
expression hs the value of the left operand after the assignment, but is
not an lvalue."

6.5.16.2/3 says: "A compound assignment of the form E1 op= E2 differs
from the simple assignment E1 = E1 op (E2) only in thta the lvalue E1 is
evaluated only once."

Finally, 6.5.3.1/2 says: "The expression ++E is equivalent to (E+=1)."
and 6.5.3.1/3 says: "The prefix -- operator is analogous to the prefix
++ operator, except that the value of the operand is decremented."

So, in C a prefix increment or decrement does no produce an lvalue.
2) I read that, in C++ postfix increment and decrement operator have
higher precedence than the prefix operator. But in C they have the
same precedence. Am I right or wrong ?
You're not exactly right, but not completely wrong either. Neither the C
nor C++ standard specifies precedence directly -- in both cases, the
precedence must be deduced from the grammar.

In both cases, the grammar specifies postfix operators with higher
precedence than prefix. In reality, however, this doesn't really matter
much -- the only time the relative precedence of a prefix and postfix
operator matters is if both are being applied to the same operand, such
as "++a++;".

In C, since the operators never produce lvalues, and the operand of the
prefix operator needs to be an lvalue, you can never have an expression
where both the pre- and post-fix operators are applied to the same
operand -- so there's never a conflict between the two that you need to
resolve via precedence.

In C++, for built-in types, the situation is the same. A user-defined
type, however, can overload the operators and return modifiable lvalues
in both cases:

For example:

#include <iostream>

class X {
int x;
public:
X(int init = 0) : x(init) {}

int &operator++() { return ++x; }
int &operator++(int) { return ++x; }

operator int() { return x; }
};

int main() {
X x;

++x++;
std::cout << x;

return 0;
}

#include <iostream>

class X {
int x;
public:
X(int init = 0) : x(init) {}

int &operator++() { return ++x; }

// well-formed but truly evil code:
int &operator++(int) { return ++x; }

operator int() { return x; }
};

int main() {
X x;

// also well-formed:
++x++;
std::cout << (int) x;

return 0;
}

Now, the difference in precedence is meaningful. We're applying both
operators to the same operand, and the higher effective precedence of
the postfix operator means the "++x++" is equivalent to:

x.operator++(int).operator++();

rather than:

x.operator++().operator++(int);

For built-in types, the postfix operators do not produce modifiable
lvalues, so an expression like this is ill-formed, and the difference in
precedence is a moot point, just like in C.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 24 '07 #5
In article <11*********************@g37g2000prf.googlegroups. com>,
su**************@yahoo.com says...

[ ... ]
1) Does it mean that prefix operator in C does not produce Lvalue in C
unlike C++ ?
That's correct -- in C 6.5.16/3 specifies that: " An assignment
expression hs the value of the left operand after the assignment, but is
not an lvalue."

6.5.16.2/3 says: "A compound assignment of the form E1 op= E2 differs
from the simple assignment E1 = E1 op (E2) only in thta the lvalue E1 is
evaluated only once."

Finally, 6.5.3.1/2 says: "The expression ++E is equivalent to (E+=1)."
and 6.5.3.1/3 says: "The prefix -- operator is analogous to the prefix
++ operator, except that the value of the operand is decremented."

So, in C a prefix increment or decrement does no produce an lvalue.
2) I read that, in C++ postfix increment and decrement operator have
higher precedence than the prefix operator. But in C they have the
same precedence. Am I right or wrong ?
You're not exactly right, but not completely wrong either. Neither the C
nor C++ standard specifies precedence directly -- in both cases, the
precedence must be deduced from the grammar.

In both cases, the grammar specifies postfix operators with higher
precedence than prefix. In reality, however, this doesn't really matter
much -- the only time the relative precedence of a prefix and postfix
operator matters is if both are being applied to the same operand, such
as "++a++;".

In C, since the operators never produce lvalues, and the operand of the
prefix operator needs to be an lvalue, you can never have an expression
where both the pre- and post-fix operators are applied to the same
operand -- so there's never a conflict between the two that you need to
resolve via precedence.

In C++, for built-in types, the situation is the same. A user-defined
type, however, can overload the operators and return modifiable lvalues
in both cases:

For example:

#include <iostream>

class X {
int x;
public:
X(int init = 0) : x(init) {}

int &operator++() { return ++x; }
int &operator++(int) { return ++x; }

operator int() { return x; }
};

int main() {
X x;

++x++;
std::cout << x;

return 0;
}

#include <iostream>

class X {
int x;
public:
X(int init = 0) : x(init) {}

int &operator++() { return ++x; }

// well-formed but truly evil code:
int &operator++(int) { return ++x; }

operator int() { return x; }
};

int main() {
X x;

// also well-formed:
++x++;
std::cout << (int) x;

return 0;
}

Now, the difference in precedence is meaningful. We're applying both
operators to the same operand, and the higher effective precedence of
the postfix operator means the "++x++" is equivalent to:

x.operator++(int).operator++();

rather than:

x.operator++().operator++(int);

For built-in types, the postfix operators do not produce modifiable
lvalues, so an expression like this is ill-formed, and the difference in
precedence is moot, just like in C.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 24 '07 #6
On Jun 24, 5:12 am, "subramanian10...@yahoo.com, India"
<subramanian10...@yahoo.comwrote:
On Jun 24, 12:06 am, Jerry Coffin <jcof...@taeus.comwrote:
Kindly clarify the following doubt also related to the Lvalue.
Consider the following code:
int main()
{
int x = 100;
++x = 1000;
return 0;
}
If I create a C source file containing this code and compile it under
both VC++2005 express edition and also gcc under Linux, I am getting
compilation error for the assignment:
++x = 1000;
error being invalid Lvalue in assignment.
However if I create a C++ source file containing this code and compile
it under VC++ 2005 and also g++, I do not get any compilation error.
Jerry has explained most of this, including the fact that this
is a difference between C and C++. I would add, however, that
in C++ the expression "++x = 1000" has undefined behavior,
because you are modifying the variable x twice without an
intervening sequence point.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Jun 24 '07 #7
su**************@yahoo.com, India wrote:
Consider

int i = 10;

Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ?

I thought both these expressions yield only values.

I am unable to understand the difference
There is no logical reason for these choices. Just avoid all non-trivial
parts of the C++ language. Or avoid the whole language... :-)

--
Dr Jon D Harrop, Flying Frog Consultancy
The OCaml Journal
http://www.ffconsultancy.com/product...ournal/?usenet
Jun 26 '07 #8
On Jun 23, 8:22 am, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
subramanian10...@yahoo.com wrote:
Consider
int i = 10;
Why do we say that ++i yields an Lvalue and i++ yields an Rvalue ?

Because they do. Why do we say that water is wet?
I thought both these expressions yield only values.

That would be incorrect. There are no such thing as "only values".
I am unable to understand the difference

You can take an address of an lvalue, you can initialise a reference
to-non-const with it, you can apply another increment to it...

int i = 10;
int *pi = &(++i);// OK
int &ri = ++i; // OK
++i--; // OK
You can't do those things with an r-value:

int i = 10;
int *pi = &(i++);// NOT OK
int &ri = i++; // NOT OK
i++--; // NOT OK

What book are you reading that doesn't explain that?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


dear victor

d statement ++i--; // OK
is not a valid one.. if u compile dis it will show "++ needs
Lvalue....
its because the postfix operator has high precedence than a prefix
operator.. so i-- will be done before ++i..
so it can be written as (++i)--;// OK ...
Jul 18 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Sergey | last post by:
Hello! Could anybody be kind enough to explain this concept? Why C++ make two ops for prefix and postfix ++ operator? As I guess, it is possible to implement both cases via sole prefix...
8
by: lovecreatesbeauty | last post by:
Hello experts, Why can this difference between prefix increment/decrement and postfix increment/decrement reside in built-in operators for built-in data types? Thanks. // test.cpp // //...
98
by: jrefactors | last post by:
I heard people saying prefix increment is faster than postfix incerement, but I don't know what's the difference. They both are i = i+1. i++ ++i Please advise. thanks!!
2
by: shan | last post by:
Hi to everybody, I am begginer in C programming language. My simple doubt is the difference between postfix & prefix unary operator plus. (i.e) i++ and ++i . plz give me an example program...
13
by: Oleg Kharitonov | last post by:
Hello! I have found an interesting thing with postfix operator ++. What should contain the variable i after exceution the following code: int i = 5; i = i++; In VC++ 7.1 and VC++ 2005...
30
by: Xah Lee | last post by:
The Concepts and Confusions of Prefix, Infix, Postfix and Fully Functional Notations Xah Lee, 2006-03-15 In LISP languages, they use a notation like “(+ 1 2)†to mean “1+2â€....
2
by: swapnaoe | last post by:
Hi, In http://elearning.embnet.org/file.php/43/ctutorial/Postfix-and-prefix----and---.html I read about postfix and prefix unary operators. It said " If the increment or decrement operator is...
3
by: news.aioe.org | last post by:
Is it possible to overload increment(++) and decrement(--) postfix and prefix operators for primitive datatypes such as int, char, short, etc. in global scope (vs as a class member function where...
2
by: puzzlecracker | last post by:
How can we implement operator ++ as postfix in prefix? thanks
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.