454,918 Members | 1,247 Online
Need help? Post your question and get tips & solutions from a community of 454,918 IT Pros & Developers. It's quick & easy.

# precedence question

 P: n/a 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 Jul 29 '05 #1
8 Replies

 P: n/a go***********@gmail.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. 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. Jul 29 '05 #2

 P: n/a Tobias Blomkvist wrote: go***********@gmail.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) Jul 29 '05 #3

 P: n/a go***********@gmail.com wrote: 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) Jul 29 '05 #4

 P: n/a Pete Becker sade: 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. Jul 29 '05 #5

 P: n/a use ++b Jul 29 '05 #6

 P: n/a use ++b Jul 29 '05 #7

 P: n/a go***********@gmail.com wrote: 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. Jul 30 '05 #8

 P: n/a go***********@gmail.com wrote: 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. Jul 31 '05 #9

### This discussion thread is closed

Replies have been disabled for this discussion.