Mailinglist Archive: opensuse-programming (32 mails)

< Previous Next >
Re: [suse-programming-e] post increment and self assignment problem
  • From: Verdi March <cincaipatron@xxxxxxx>
  • Date: Sat, 6 Jul 2002 18:52:07 +0200 (MEST)
  • Message-id: <31563.1025974327@xxxxxxxxxxxxx>
Hi,
from the result (print x=11), post increment is evaluated after
assignment (agree with you), meaning the post increment operator
is not higher than assignment. But I decided to post to this list
is because on my reference book, post increment has higher
precedence. Actually this is the source of my confusion...


Regards,
Verdi

> Hello,
> The postincrement operator does NOT have a higher priority than
> assignment. First, the assignment is done, then the increment is done.
> The preincrement operator has a higher presedence than assignment
> (++x). With preincrement, first the variable is incremented then the
> result is assigned.
> And your problem is not with operator presedence. Let me explain you
> what happens for x step by step:
> 1. x is created in the memory and the value 10 is assigned
> 2. x is assigned to x, in other words value of x is set to value of x
> (so x is 10)
> 3. The value stored in x is incremented by 1 (so now x is 11)
> 4. x is printed to the stdout (the printed value is 11)
> In your code it seems as if you are trying to print the old value of x
> (the value before the increment). For this to happen you should use
>
> z = x++;
> cout << "x = " << z << '\n';
>
> instead of
>
> x = x++;
> cout << "x = " << x << '\n';
>
> This way you will be able to print the old value. To make the things
> quite clear
> 1. x is created in the memory and the value 10 is assigned
> 2. x is assigned to z, in other words value of z is set to value of x
> (so z is 10)
> 3. The value stored in x is incremented by 1 (so now x is 11)
> 4. z is printed to the stdout (the printed value is 10).
>
> I hope my explanation contains the answer to your question.

[del]


> --
> Sedat Dogru
> __________________________________________________
> blue tec IT services ag
>
> Sedat Dogru
> Project Manager
>
> Kleinhadernerstr. 40
> 80689 München
> Germany
>
> +49 - 89 - 700 76 43- 46
>
> mail: sdogru@xxxxxxxxxxxx
> internet: www.blue-tec.net

--
GMX - Die Kommunikationsplattform im Internet.
http://www.gmx.net


< Previous Next >
List Navigation
References