bug-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Arithmetic expression: evaluation order bug


From: Alain D D Williams
Subject: Re: Arithmetic expression: evaluation order bug
Date: Thu, 29 Dec 2022 20:45:11 +0000
User-agent: Mutt/1.10.1 (2018-07-13)

On Thu, Dec 29, 2022 at 09:09:25PM +0100, Emanuele Torre wrote:
> On Thu, Dec 29, 2022 at 05:35:48PM +0000, Alain D D Williams wrote:
> > On Thu, Dec 29, 2022 at 06:23:09PM +0100, Steffen Nurpmeso wrote:
> > > Hello.
> > > 
> > >   Name:         bash
> > >   Path:         /usr/ports/core
> > >   Version:      5.2.15
> > >   Release:      1
> > > 
> > >   $ i=10 j=20;echo $(( i += j += i += j ));echo $i,$j
> > >   60
> > >   60,50
> > >   $ i=10 j=20;echo $(( i += j += i += i ));echo $i,$j
> > >   50
> > >   50,40
> > 
> > You are modifying something that is used elsewhere in an expression. I am 
> > not
> > surprised that you do not get what you expect; others might expect something
> > different.
> 
> I don't think that is correct.
> 
> Unlike  a++ - --a  which is unspecified in C because the order of
> evaluation of the lhs and rhs of the - operator is unspecified, the
> order of evaluation of  i += j += i += i  or  i += (j += (i += i)) is
> well defined and in no way ambiguous.

No. i += j += i += i does not contain a sequence point so there is no guarantee
that anything is completed (eg storing a value in variable i) before another
part (getting a value from variable i) is evaluated.

https://stackoverflow.com/questions/3575350/sequence-points-in-c

https://c-faq.com/expr/seqpoints.html

-- 
Alain Williams
Linux/GNU Consultant - Mail systems, Web sites, Networking, Programmer, IT 
Lecturer.
+44 (0) 787 668 0256  https://www.phcomp.co.uk/
Parliament Hill Computers Ltd. Registration Information: 
https://www.phcomp.co.uk/Contact.html
#include <std_disclaimer.h>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]