Is i = i++; bad style?

  • Thread starter lovecreatesbea...
  • Start date
L

lovecreatesbea...

i = i++;

The gcc gives out a warning on this line of code, saying "warning:
operation on `i' may be undefined".

But I think, at the end of the execution of the statement, the
variable i is eventually increased by one, am I right?
 
L

lovecreatesbea...

i = i++;

The gcc gives out a warning on this line of code, saying "warning:
operation on `i' may be undefined".

But I think, at the end of the execution of the statement, the
variable i is eventually increased by one, am I right?

As a part of a compound expression, i = i++ (an expression, no
semocolon followed) may be undefined value, right?
 
R

Robert Gamble

As a part of a compound expression, i = i++ (an expression, no
semocolon followed) may be undefined value, right?

It is undefined both as part of a larger expression and as a
standalone expression. 9899:1999 §6.5p2 reads:

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

Assignment does not introduce a sequence point so i is modified twice
without an intervening sequence point, once by the ++ operator and
once by the assignment. Violation of a "shall" outside of a
constraint (as is the case here) causes undefined behavior. See also
<http://www.c-faq.com/> question 3.3.

Robert Gamble
 
F

Francine.Neary

i = i++;

The gcc gives out a warning on this line of code, saying "warning:
operation on `i' may be undefined".

But I think, at the end of the execution of the statement, the
variable i is eventually increased by one, am I right?

What an original post! No one's ever thought to ask whether i=i++
makes sense before. No point searching Google or the C FAQ to find out
- this is new territory for C-programming humanity.
 
C

CBFalconer

i = i++;

The gcc gives out a warning on this line of code, saying "warning:
operation on `i' may be undefined".

But I think, at the end of the execution of the statement, the
variable i is eventually increased by one, am I right?

No. The result is undefined. Incrementing by one is a
possibility. Just use "i++;" alone for an accurate increment.
 
L

lovecreatesbea...

What an original post! No one's ever thought to ask whether i=i++
makes sense before. No point searching Google or the C FAQ to find out
- this is new territory for C-programming humanity.

Don't you think the following program prints 11 on any or your system?
Isn't it portable?

int main(void)
{
int i;
i = 10;
i = i++;
printf("%d\n", i);
return 0;
}
 
R

ranjeet.gupta

i = i++;

The gcc gives out a warning on this line of code, saying "warning:
operation on `i' may be undefined".

But I think, at the end of the execution of the statement, the
variable i is eventually increased by one, am I right?

Yes you are right, but diffrent compiler may behave in diffrent way.
in short it is compiler dependent.

~Ranjeet
 
D

Denis Kasak

Don't you think the following program prints 11 on any or your system?
Isn't it portable?

int main(void)
{
int i;
i = 10;
i = i++;
printf("%d\n", i);
return 0;
}

No, it's not. It invokes undefined behaviour for reasons already stated
elsewhere on this thread (no intervening sequence point between two
modifications of a same variable). It could print anything, not anything
at all, or even do something completely unrelated to printing.
 
F

Flash Gordon

Yes you are right, but diffrent compiler may behave in diffrent way.
in short it is compiler dependent.

Or a different version of the same compiler, or the same compiler with
different options, or the same compiler with the same options but
something different in the source code which is apparently completely
unrelated (yes, I have seen apparently unrelated code have effects on
the code of interest, that's the fun thing with optimisers). In short,
the OP was correct ONLY for the specific run that the OP did where that
was the observed result, in general (even with the same compiler etc)
the OP is WRONG, because the only thing that can be said with certainty
is that it invokes undefined behaviour.

By the way, since the OP has been around for a long time off and on yet
is asking the question I think it is either a troll or fundamentally
incapable of learning to program in C. The meaning of undefined
behaviour was even explained to the OP back in 2005
http://groups.google.co.uk/group/co...6ce4?lnk=st&q=&rnum=33&hl=en#c54158409a166ce4
 
L

lovecreatesbea...

<http://www.c-faq.com/>, question 3.3.

Sheesh.

--
Keith Thompson (The_Other_Keith) (e-mail address removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

I had this question when I was reading the c-faq. As for

i = i++;

I ever thought it works in following two ways.

1.
i = i /*no sequencing point followed immediately*/
i++
; /*sequencing point */

So, result: i = i + 1;

2.
i++ /*no sequencing point followed immediately,
but will the increasing effect be lost?*/
i = i /*i was assigned to itself*/
; /*sequencing point */

So, result: i = i;

And the result isn't certain.

Do I think it correctly?
 
L

lovecreatesbea...

(e-mail address removed) wrote, On 06/07/07 06:45:
By the way, since the OP has been around for a long time off and on yet
is asking the question I think it is either a troll or fundamentally
incapable of learning to program in C. The meaning of undefined
behaviour was even explained to the OP back in 2005http://groups.google.co.uk/group/comp.lang.c/browse_frm/thread/2b0e0f...

Why are you so impolite and unkind to people?
 
J

J. J. Farrell

Don't you think the following program prints 11 on any or your system?

No. Since its behaviour is undefined, I don't know if it will compile
at all, or if it will print anything at all, let alone if it will
print 11. Since its behaviour is undefined, I don't care what it does,
and don't care to waste my time finding out.
Isn't it portable?

No, as you've been repeatedly told.
 
R

Richard Heathfield

Robert Gamble said:
It is undefined both as part of a larger expression and as a
standalone expression.

Please don't feed the trolls. This guy has been around clc for quite
long enough (like, years) that he ought to know by now where to find
the FAQ, and that he must have learned the answer to his question a
dozen times or more.
 
L

lovecreatesbea...

Robert Gamble said:







Please don't feed the trolls. This guy has been around clc for quite
long enough (like, years) that he ought to know by now where to find
the FAQ, and that he must have learned the answer to his question a
dozen times or more.

Man, why do you always keep angry? Can you be reasonable? Are you the
god on the net?
 
R

Richard Bos

What an original post! No one's ever thought to ask whether i=i++
makes sense before. No point searching Google or the C FAQ to find out
- this is new territory for C-programming humanity.

The OP is either a more subtle and consistent troll than usual, or
entirely bereft of common sense and learning ability. Either way,
ignoring it seems the best idea.

Richard
 
L

lovecreatesbea...

The OP is either a more subtle and consistent troll than usual, or
entirely bereft of common sense and learning ability. Either way,
ignoring it seems the best idea.

Since you won't participate in the discussion or help the OP out of
the trouble, why can't you send Francine.Ne a personal email to remind
him about this, or just shut up?
 
C

Chris Dollin


Run away! Run away!

No, not /that/ way! Toward the FAQ!
The gcc gives out a warning on this line of code, saying "warning:
operation on `i' may be undefined".

(fx:applause)

"May" is a tad on the weak side, though.
But I think, at the end of the execution of the statement, the
variable i is eventually increased by one, am I right?

No. You're not right.

It's /possible/ that `i` is increased by 1. It's /possible/ that
it's decreased by 2. It's /possible/ that it's set to zero, or
bitwise-inverted, or that an exception is thrown, or that execution
is halted, or that all local variables are cleared, or that it
prints the message "The rest is silence", or that your executable
is deleted, or that your display crashes.

Anything. It's all permitted by the standard. Anything. It doesn't
even have to be physically possible.
 
C

CBFalconer

Richard said:
(e-mail address removed) wrote:
.... snip ...

The OP is either a more subtle and consistent troll than usual, or
entirely bereft of common sense and learning ability. Either way,
ignoring it seems the best idea.

'lovecreatesbeauty' just attained my PLONK list. Bye-bye.
 

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,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top