473,425 Members | 1,808 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,425 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 7811
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
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.