i = i++;

R

rahul

int i = 0;
i = i++;
I noticed that some people say this is undefined as the value of a
variable can not be accessed twice between two sequence points.
But I think the standard says that a variable can not be modified
twice between two sequence points. And in this case, it is modified
just once. So,
a = a++ + a++;
is undefined but not i = i++.
Correct me if I am wrong as I don't own a copy of the standards. I
read this stuff in some book.
 
R

Richard Bos

rahul said:
int i = 0;
i = i++;
I noticed that some people say this is undefined as the value of a
variable can not be accessed twice between two sequence points.
But I think the standard says that a variable can not be modified
twice between two sequence points. And in this case, it is modified
just once. So,
a = a++ + a++;
is undefined but not i = i++.
Correct me if I am wrong

You're wrong.
as I don't own a copy of the standards.

That, too, is wrong. Download the latest draft (of the full Standard
plus the last TC); do a websearch for n1256.pdf.
I read this stuff in some book.

Read the FAQ instead. Start at section 3:
<http://c-faq.com/expr/index.html>.

Richard
 
B

Bartc

rahul said:
int i = 0;
i = i++;
I noticed that some people say this is undefined as the value of a
variable can not be accessed twice between two sequence points.
But I think the standard says that a variable can not be modified
twice between two sequence points. And in this case, it is modified
just once. So,
a = a++ + a++;
is undefined but not i = i++.
Correct me if I am wrong as I don't own a copy of the standards.

Clearly the variable i is modified twice: once to increment it, and again to
assign to it (not necessarily in that order).
 
W

Willem

Bartc wrote:
)
) )> int i = 0;
)> i = i++;
)> I noticed that some people say this is undefined as the value of a
)> variable can not be accessed twice between two sequence points.
)> But I think the standard says that a variable can not be modified
)> twice between two sequence points. And in this case, it is modified
)> just once. So,
)> a = a++ + a++;
)> is undefined but not i = i++.
)> Correct me if I am wrong as I don't own a copy of the standards.
)
) Clearly the variable i is modified twice: once to increment it, and again to
) assign to it (not necessarily in that order).

But, j = i + i++; is also undefined AFAIK.


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
 
R

Richard Tobin

Willem said:
)> a = a++ + a++;
)> is undefined but not i = i++.
)> Correct me if I am wrong as I don't own a copy of the standards.
) Clearly the variable i is modified twice: once to increment it, and again to
) assign to it (not necessarily in that order).
But, j = i + i++; is also undefined AFAIK.

Yes, there are two rules:

Between the previous and next sequence point an object shall have
its stored value modified at most once by the evaluation of an
expression. Furthermore, the prior value shall be read only to
determine the value to be stored.

"j = i + i++" violates the second one. i is read both to determine the
new value of i in the second operand of +, and to determine the value of
the first operand.

-- Richard
 
B

Barry Schwarz

int i = 0;
i = i++;
I noticed that some people say this is undefined as the value of a
variable can not be accessed twice between two sequence points.
But I think the standard says that a variable can not be modified
twice between two sequence points. And in this case, it is modified
just once. So,
a = a++ + a++;
is undefined but not i = i++.
Correct me if I am wrong as I don't own a copy of the standards. I
read this stuff in some book.

You are wrong on several levels.

Your code modifies i more than once so even with your incomplete
understanding of the restriction you should be able to see it is
undefined.

Don't guess what the standard says. There are numerous draft copies
available for download (google for n1124 as an example).

Or you could look through the google archives of this group to see the
hundreds of times it has been addressed before.

If your book doesn't give you the complete restriction, throw it away
and look through the archives of this group for book recommendations.


Remove del for email
 
K

Keith Thompson

CBFalconer said:
You can get quite adequate C standard copies over the net. See the
things marked "C99" below. The text version is the smallest, and I
consider handiest.

The text version is also a pre-standard draft of C99, and differs in
some (mostly minor, I think) ways from the actual standard. If you
don't mind dealing with PDF rather than plain text, I recommend
n1256.pdf. I'd suggest n869.txt (or rather n869_txt.bz2) only if you
have serious problems with PDF documents.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,175
Messages
2,570,942
Members
47,489
Latest member
BrigidaD91

Latest Threads

Top