 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
gouqizi.lvcha@gmail.com Guest
|
Posted: Fri Jul 29, 2005 7:59 pm Post subject: precedence question |
|
|
I have a statement as follows,
a = b++;
why b=b+1 after a=b. I check the C language precedence (K&R Page 52) ,
++ should has higher precedence than = .
Rick
|
|
| Back to top |
|
 |
Tobias Blomkvist Guest
|
Posted: Fri Jul 29, 2005 8:11 pm Post subject: Re: precedence question |
|
|
[email]gouqizi.lvcha (AT) gmail (DOT) com[/email] sade:
| Quote: | I have a statement as follows,
a = b++;
why b=b+1 after a=b. I check the C language precedence (K&R Page 52) ,
++ should has higher precedence than = .
Rick
|
Perhaps you should check the *C++ Standard* instead.
Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
|
|
| Back to top |
|
 |
Tobias Blomkvist Guest
|
Posted: Fri Jul 29, 2005 8:23 pm Post subject: Re: precedence question |
|
|
Pete Becker sade:
| Quote: |
Doesn't matter: they both require that behavior.
|
I wasn't implying anything else.
Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
|
|
| Back to top |
|
 |
msh Guest
|
Posted: Fri Jul 29, 2005 10:05 pm Post subject: Re: precedence question |
|
|
use ++b
|
|
| Back to top |
|
 |
msh Guest
|
Posted: Fri Jul 29, 2005 10:06 pm Post subject: Re: precedence question |
|
|
use ++b
|
|
| Back to top |
|
 |
Alan Johnson Guest
|
Posted: Sat Jul 30, 2005 1:32 am Post subject: Re: precedence question |
|
|
[email]gouqizi.lvcha (AT) gmail (DOT) com[/email] wrote:
| Quote: | I have a statement as follows,
a = b++;
why b=b+1 after a=b. I check the C language precedence (K&R Page 52) ,
++ should has higher precedence than = .
Rick
|
Let's define a new operator, @. @ has the following semantics. The
expression @x evaluates to the value x+1. The expression x@ evaluates
to the value of x. In neither case does the value of x change.
Now, let's let x = 3. What are the values of x, y and z after the
following assignments?
y = @x ;
z = x@ ;
Obviously, x = 3, y = 4 and z = 3.
The only difference between the @ operator I made up, and the ++
operator is that ++ has a side effect of incrementing the variable to
which it is applied. It still holds that ++x evalutes to the value x+1,
and x++ evalutes to the value of x.
Now, to answer your question, "a=b" doesn't happen anywhere, never, not
at all, not before "b++", and not after "b++". What happens is that,
first, the expression "b++" is evaluated. This expression evaluates to
whatever the value of b is (before incrementing). It makes no
difference that as a side effect b then gets incremented. So then, the
result of of the expression "b++" (which is b's old value) gets assigned
to a.
|
|
| Back to top |
|
 |
Ron Natalie Guest
|
Posted: Sun Jul 31, 2005 12:26 pm Post subject: Re: precedence question |
|
|
[email]gouqizi.lvcha (AT) gmail (DOT) com[/email] wrote:
| Quote: | I have a statement as follows,
a = b++;
why b=b+1 after a=b. I check the C language precedence (K&R Page 52) ,
++ should has higher precedence than = .
Precedence is a different concept from order of evaluation. |
Further, order of evaluation is a different concept than the rules
for updating variables in an expression.
Precedence is the rules for interpreting a statement. That ++ has
a higher precedence than = just means that the interpretation of
the above is:
a = (b++)
and not
(a = b) ++
C++ doesn't mandate any particular order of evaluation in most cases.
The compiler is free to reorder the processing of subexpressions to
make things optimal. Further until you hit a sequence point (which
above is at the end of the full expression), there's no guarantee of
when the variables will be changed.
Anyhow, in your case the expression b++ is defined to be the value of
b BEFORE the increment. The value of ++b is the value before the
increment PLUS 1. There's no order of evaluation involved here,
just the meaning of the operators.
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Mon Aug 29, 2005 8:13 pm Post subject: Re: precedence question |
|
|
Tobias Blomkvist wrote:
| Quote: | gouqizi.lvcha (AT) gmail (DOT) com sade:
I have a statement as follows,
a = b++;
why b=b+1 after a=b. I check the C language precedence (K&R Page 52) ,
++ should has higher precedence than = .
Rick
Perhaps you should check the *C++ Standard* instead.
|
Doesn't matter: they both require that behavior.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
|
|
| Back to top |
|
 |
Pete Becker Guest
|
Posted: Mon Aug 29, 2005 8:14 pm Post subject: Re: precedence question |
|
|
[email]gouqizi.lvcha (AT) gmail (DOT) com[/email] wrote:
| Quote: | I have a statement as follows,
a = b++;
why b=b+1 after a=b. I check the C language precedence (K&R Page 52) ,
++ should has higher precedence than = .
|
Read the description of what postfix ++ does.
--
Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|