473,395 Members | 1,668 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

Jul 29 '05 #1
8 2120
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
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
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
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
msh
use ++b

Jul 29 '05 #6
msh
use ++b

Jul 29 '05 #7
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Andy White | last post by:
the statement: foo.b = 7; indicates the first element of the array member of the data structure. My question is, why aren't the brackets evaluated first then the dot operator after since the...
4
by: Master of C++ | last post by:
Hi, This is a simple question. In the following example, .. class Vector .. { .. private: .. int *Numbers; .. int vLength; ..
3
by: maadhuu | last post by:
hi, i am a bit confused as to how *i++ (i is a pointer) works.....the postfix + has a higher precedence than prefix ++ and i think precedence is from left to right whereas ,prefix ++ and * have...
21
by: siliconwafer | last post by:
Hi, In case of following expression: c = a && --b; if a is 0,b is not evaluated and c directly becomes 0. Does this mean that && operator is given a higher precedence over '--'operator? as...
11
by: Rupesh | last post by:
Hello all, See the code .... int i=-3,j=2,k=0,m; m=++i;&&++j||++k; printf ("%d %d %d %d",i,j,k,m); I executed this code on gcc. the o/p i had got is:- -2 3 0 1
5
by: mrpermuted | last post by:
Hello, Could someone explain to me why the postfix ++ operator is listed near the top of the operator precedence (above even prefix ++ on my book's chart) but is not evaluated before operators...
36
by: Frederick Gotham | last post by:
sizeof has the same precedence as a cast, and they both bind from right to left. The following won't compile for me with gcc: int main(void) { sizeof(double)5; return 0; }
5
by: junky_fellow | last post by:
Hi, I have a very basic doubt about associativity and precedence of operators. I am not a computer science person and may find it quite weird. Consider an expression 4+5*2
7
by: linq936 | last post by:
Hi, I am puzzled on this C puzzle: How to interpret this C statement: sizeof (int) * p Is that the size of integer type multiplies p or the size of whatever p points to in integer type? I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.