473,565 Members | 2,770 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2131
go***********@g mail.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***********@g mail.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***********@g mail.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***********@g mail.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***********@g mail.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
1955
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 brackets are higher on the precedence chart? Thanks
4
3205
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
1696
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 same precedence and think its from right to left...... well, can someone plss enlighten me how this works ?? thanx.
21
3007
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 opposed to what is mentioned in precedence table? Also, with comma operator. consider,
11
1534
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
1978
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 of lower precedence? It seems like a contradiction to me. Thanks, Glen
36
2771
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
2528
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
1943
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 think it should be the latter because there are 4 parser tokens here, sizeof, int, * and p, the first and the second are of same precedence which are...
0
7666
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7888
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8108
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
7951
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6260
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3626
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2083
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1201
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.