precedence question

G

gouqizi.lvcha

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
 
T

Tobias Blomkvist

(e-mail address removed) 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.

Tobias
 
A

Alan Johnson

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

Ron Natalie

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

Pete Becker

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.
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top