Velocity Reviews > Increment operator precedence

# Increment operator precedence

nl
Guest
Posts: n/a

 02-15-2008
Hi, I am confused about the order of operation for the increment
operater ++. I wrote the program below trying to clear my doubts, but
it just got me more confused. For example, in the second case, I
thought that ++x will increment x to 2. and since y = 3, that makes z
= 2 + 3 =5. However the result shows that z =4.

Would greatly appreciate any help.

#include <stdio.h>

int main(void)
{
int x=1, y=1, z=0;

z = x++ +( y += x++) ;

printf("\n\n%d %d %d\n\n", x, y, z);
x=1, y=1, z=0;

z = x++ +( y += ++x) ;
printf("\n\n%d %d %d\n\n", x, y, z);

x=1, y=1, z=0;

z = ++x +( y += x++) ;
printf("\n\n%d %d %d\n\n", x, y, z);

x=1, y=1, z=0;
z = ++x +( y += ++x) ;

printf("\n\n%d %d %d\n\n", x, y, z);
return 0;
}

The results:

2 2 3

2 3 4

3 3 5

3 4 6

raghu
Guest
Posts: n/a

 02-15-2008
On Feb 15, 12:09*pm, nl <(E-Mail Removed)> wrote:
> Hi, I am confused about the order of operation for the increment
> operater ++. I wrote the program below trying to clear my doubts, but
> it just got me more confused. For example, in the second case, I
> thought that ++x will increment x to 2. and since y = 3, that makes z
> = 2 + 3 =5. However the result shows that z =4.
>
> Would greatly appreciate any help.
>
> #include <stdio.h>
>
> int main(void)
> {
> * * int x=1, y=1, z=0;
>
> * * z = x++ +( y += x++) ;
>
> * * printf("\n\n%d %d %d\n\n", x, y, z);
> * * x=1, y=1, z=0;
>
> * * z = x++ +( y += ++x) ;
> * * printf("\n\n%d %d %d\n\n", x, y, z);
>
> * * x=1, y=1, z=0;
>
> * * z = ++x +( y += x++) ;
> * * printf("\n\n%d %d %d\n\n", x, y, z);
>
> * * x=1, y=1, z=0;
> * * z = ++x +( y += ++x) ;
>
> * * printf("\n\n%d %d %d\n\n", x, y, z);
> * * return 0;
>
> }
>
> The results:
>
> 2 2 3
>
> 2 3 4
>
> 3 3 5
>
> 3 4 6

The results are correct.

Where did u find the problem?

Ian Collins
Guest
Posts: n/a

 02-15-2008
nl wrote:
> Hi, I am confused about the order of operation for the increment
> operater ++. I wrote the program below trying to clear my doubts, but
> it just got me more confused. For example, in the second case, I
> thought that ++x will increment x to 2. and since y = 3, that makes z
> = 2 + 3 =5. However the result shows that z =4.
>
> Would greatly appreciate any help.
>
> #include <stdio.h>
>
> int main(void)
> {
> int x=1, y=1, z=0;
>
> z = x++ +( y += x++) ;
>

The above invokes undefined behaviour, you can't increment x twice
without a sequence point.

--
Ian Collins.

Nick Keighley
Guest
Posts: n/a

 02-15-2008
On 15 Feb, 08:25, raghu <(E-Mail Removed)> wrote:
> On Feb 15, 12:09*pm, nl <(E-Mail Removed)> wrote:

> > Hi, I am confused about the order of operation for the increment
> > operater ++. I wrote the program below trying to clear my doubts, but
> > it just got me more confused. For example, in the second case, I
> > thought that ++x will increment x to 2. and since y = 3, that makes z
> > = 2 + 3 =5. However the result shows that z =4.

>
> > Would greatly appreciate any help.

>
> > #include <stdio.h>

>
> > int main(void)
> > {
> > * * int x=1, y=1, z=0;

>
> > * * z = x++ +( y += x++) ;

>
> > * * printf("\n\n%d %d %d\n\n", x, y, z);
> > * * x=1, y=1, z=0;

>
> > * * z = x++ +( y += ++x) ;
> > * * printf("\n\n%d %d %d\n\n", x, y, z);

>
> > * * x=1, y=1, z=0;

>
> > * * z = ++x +( y += x++) ;
> > * * printf("\n\n%d %d %d\n\n", x, y, z);

>
> > * * x=1, y=1, z=0;
> > * * z = ++x +( y += ++x) ;

>
> > * * printf("\n\n%d %d %d\n\n", x, y, z);
> > * * return 0;

>
> > }

>
> > The results:

>
> > 2 2 3

>
> > 2 3 4

>
> > 3 3 5

>
> > 3 4 6

>
> The results are correct.

no. All the instances in the example invoke Undefined Behaviour
so the implementation can do whatever it pleases. In a sense *any*
result is "correct". I'd prefer to say expecting any particular
result is wrong.

> Where did u find the problem

please don't use "txt" speak in your posts.

--
Nick Keighley

Chris Dollin
Guest
Posts: n/a

 02-15-2008
nl wrote:

> Hi, I am confused about the order of operation for the increment
> operater ++. I wrote the program below trying to clear my doubts, but
> it just got me more confused. For example, in the second case, I
> thought that ++x will increment x to 2. and since y = 3, that makes z
> = 2 + 3 =5. However the result shows that z =4.
>
> Would greatly appreciate any help.
>
> #include <stdio.h>
>
> int main(void)
> {
> int x=1, y=1, z=0;
>
> z = x++ +( y += x++) ;

Not allowed.

> printf("\n\n%d %d %d\n\n", x, y, z);
> x=1, y=1, z=0;
>
> z = x++ +( y += ++x) ;

Not allowed.

> printf("\n\n%d %d %d\n\n", x, y, z);
>
> x=1, y=1, z=0;
>
> z = ++x +( y += x++) ;

Not allowed.

> printf("\n\n%d %d %d\n\n", x, y, z);
>
> x=1, y=1, z=0;
> z = ++x +( y += ++x) ;

(All together now Not allowed.
>
> printf("\n\n%d %d %d\n\n", x, y, z);
> return 0;
> }

All four statements lead to Undefined Behaviour: C does not specify and
does not care how the implementation chooses to implement them. Taking

> z = x++ +( y += x++) ;

as the example, we see it tries to update `x` twice in the same
statement. This is /specifically/ not allowed by the Standard [1].
(I believe this is to allow implementors enough slack to do efficient
code generation, but it also eliminates some chances of confusion.)

In the expression `a + b`, it's not specified whether `a` or `b` is
evaluated first. In the expressions `a++` and `++a, it's not specified
when `a` is incremented, so long as its done by the end of the statement.
In the expression `a = b`, it's not specified whether (the address of)
`a` is evaluated before (the value of) `b`. So even without the Thou
Shalt Not for multiple updates of `x`, in

> int x=1, y=1, z=0;
> z = x++ +( y += x++) ;

`x`, `y`, and `z` could end up with all sorts of different values;
that in fact they are allowed to end up with /any values at all/,
or an exception could be thrown [2], or /other/ variables could
be updated, is worse but not concept-breakingly worse.

[1] Actually the Standard talks about "sequence points", which are
the points in an expression where any side-effects of the
expression must have been committed; end-of-statement is the
most common, but the others are the end of the left operand of
`&&`, `||`, `?:`, the comma-operator `,` (NOT the argument-
separator `,`),, and function-call.

[2] Yes, C doesn't have exceptions -- but Undefined Behaviour takes
you outside where C cares ...

--
"It was the dawn of the third age of mankind." /Babylon 5/

Hewlett-Packard Limited Cain Road, Bracknell, registered no:
registered office: Berks RG12 1HN 690597 England

John Bode
Guest
Posts: n/a

 02-15-2008
On Feb 15, 2:09 am, nl <(E-Mail Removed)> wrote:
> Hi, I am confused about the order of operation for the increment
> operater ++. I wrote the program below trying to clear my doubts, but
> it just got me more confused. For example, in the second case, I
> thought that ++x will increment x to 2. and since y = 3, that makes z
> = 2 + 3 =5. However the result shows that z =4.
>

As others have pointed out, attempting to update the same object more
than once between sequence points results in undefined behavior; *any*
result is considered "correct", because the standard places no
restrictions on the compiler.

Any expression of the forms

x = x++
i++ + i++
a[i] = i++ (or a[i++] = i)

will result in undefined behavior.

Some things to remember when using the autoincrement and autodecrement
operators:

1. Both a++ and a-- evaluate to the current value of a, ++a evaluates
to the current value of a + 1, --a evaluates to the current value of a
- 1, and as a *side effect*, the value of a is updated. Note that the
side effect does not have to be applied immediately after the
expression has been evaluated. Depending on the compiler, side
effects may be deferred until all other expressions have been
evaluated, or until some subset of expressions have been evaluated.
For example, given the statement

z = a++ * --b;

z will get the current value of a multiplied by the current value of
b-1. However, exactly when a and b get updated is not fixed; both may
be updated immediately after they're evaluated, they may be updated
only after both have been evaluated, or they may be updated after z
has been assigned. The only rule is that both have been updated by
the end of the statement. Any of the following are possible:

t1 <- a // a and b are updated immediately after evaluation
a <- a + 1
t2 <- b - 1
b <- b - 1
z <- t1 * t2

or

t1 <- a // a and b are updated at the end
t2 <- b - 1
z <- t1 * t2
a <- a + 1
b <- b - 1

2. a-- evaluates to the current value of a, --a evaluates to the
current value of a - 1, and as a *side effect*, the value of a is
decremented. Same disclaimer as above about when the side effect is
applied.

3. Precedence and order of evaluation are two different things.
Given the expression

a() * b() + c()

precedence rules require that the result of a() be multiplied by the
result of b() before the result of c() is added. However, there's no
guarantee that a() will be *evaluated* before b() or c(). The
compiler may arrange things so that c() is called *first*, and its
result is stored until a() and b() have been evaluated and their
results multiplied together. Similarly, with an expression like

a++ * b++ + c++

there's no guarantee that a has been evaluated or incremented before
either c or b have been evaluated.

CBFalconer
Guest
Posts: n/a

 02-15-2008
nl wrote:
>
> Hi, I am confused about the order of operation for the increment
> operater ++. I wrote the program below trying to clear my doubts,
> but it just got me more confused. For example, in the second
> case, I thought that ++x will increment x to 2. and since y = 3,
> that makes z = 2 + 3 =5. However the result shows that z =4.
>
> Would greatly appreciate any help.
>
> #include <stdio.h>
>
> int main(void) {
> int x=1, y=1, z=0;
>
> z = x++ +( y += x++) ;

^ ^
[1] [2]

Why do you assume that [1] precedes [2], or that [2] precedes [1]?
If you read the C standard you will see that the expression is
undefined in standard C. Any result meets the requirements.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home.att.net>
Try the download section.

--
Posted via a free Usenet account from http://www.teranews.com

Jack Klein
Guest
Posts: n/a

 02-16-2008
On Fri, 15 Feb 2008 07:22:16 -0800 (PST), John Bode
<(E-Mail Removed)> wrote in comp.lang.c:

> On Feb 15, 2:09 am, nl <(E-Mail Removed)> wrote:
> > Hi, I am confused about the order of operation for the increment
> > operater ++. I wrote the program below trying to clear my doubts, but
> > it just got me more confused. For example, in the second case, I
> > thought that ++x will increment x to 2. and since y = 3, that makes z
> > = 2 + 3 =5. However the result shows that z =4.
> >

>
> As others have pointed out, attempting to update the same object more
> than once between sequence points results in undefined behavior; *any*
> result is considered "correct", because the standard places no
> restrictions on the compiler.
>
> Any expression of the forms
>
> x = x++
> i++ + i++
> a[i] = i++ (or a[i++] = i)
>
> will result in undefined behavior.
>
> Some things to remember when using the autoincrement and autodecrement
> operators:

ITYM "preincrement" and "predecrement", rather than "autoincrement"
and "autodecrement".

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

Kenneth Brody
Guest
Posts: n/a

 02-17-2008
raghu wrote:
>
> On Feb 15, 12:09 pm, nl <(E-Mail Removed)> wrote:
> > Hi, I am confused about the order of operation for the increment
> > operater ++. I wrote the program below trying to clear my doubts, but

[...]
> > z = x++ +( y += x++) ;

[...]
> > z = x++ +( y += ++x) ;

[...]
> > z = ++x +( y += x++) ;

[...]
> > z = ++x +( y += ++x) ;

[...]
> > The results:
> >
> > 2 2 3
> >
> > 2 3 4
> >
> > 3 3 5
> >
> > 3 4 6

>
> The results are correct.

Well, "correct" in the sense that "undefined behavior can result
in anything happening, including what you thought you meant to do".

> Where did u find the problem?

I don't believe that Mr. Thant was looking for any problems.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <(E-Mail Removed)>

John Bode
Guest
Posts: n/a

 02-18-2008
On Feb 15, 10:30 pm, Jack Klein <(E-Mail Removed)> wrote:
> On Fri, 15 Feb 2008 07:22:16 -0800 (PST), John Bode
> <(E-Mail Removed)> wrote in comp.lang.c:
>
>
>
> > On Feb 15, 2:09 am, nl <(E-Mail Removed)> wrote:
> > > Hi, I am confused about the order of operation for the increment
> > > operater ++. I wrote the program below trying to clear my doubts, but
> > > it just got me more confused. For example, in the second case, I
> > > thought that ++x will increment x to 2. and since y = 3, that makes z
> > > = 2 + 3 =5. However the result shows that z =4.

>
> > As others have pointed out, attempting to update the same object more
> > than once between sequence points results in undefined behavior; *any*
> > result is considered "correct", because the standard places no
> > restrictions on the compiler.

>
> > Any expression of the forms

>
> > x = x++
> > i++ + i++
> > a[i] = i++ (or a[i++] = i)

>
> > will result in undefined behavior.

>
> > Some things to remember when using the autoincrement and autodecrement
> > operators:

>
> ITYM "preincrement" and "predecrement", rather than "autoincrement"
> and "autodecrement".
>

I've always used "auto*crement" when referring to both pre- and
postfix forms, and IINM the rules I listed apply to both.

Am I abusing terminology again?

 Thread Tools

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is OffTrackbacks are On Pingbacks are On Refbacks are Off Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Alf P. Steinbach /Usenet C++ 0 05-22-2011 12:03 PM jrefactors@hotmail.com C++ 99 06-11-2010 12:51 PM Peng Yu Perl Misc 7 11-23-2008 11:44 PM jrefactors@hotmail.com C Programming 104 10-27-2005 11:44 PM David Frauzel Perl 2 05-16-2004 11:19 PM

Advertisments