On 24 Apr., 14:46, Robert Peirce <b...@[EMAIL PROTECTED]
>
wrote:
> In article <funkla$p3...@[EMAIL PROTECTED]
>,
> =A0Janis Papanagnou <Janis_Papanag...@[EMAIL PROTECTED]
> wrote:
>
> > Robert Peirce wrote:
>
> > > Frankly, it never occurred to me that these would be different. =A0I
s=
till
> > > have trouble figuring out the logic behind the line that produced
28. =
=A0
> > > 25 made sense, but not 28.
>
> > Why? If b was 13 and is pre-incremented before the rest of the
> > expression is evaluated then you have 14 + 14 =3D 28.
>
> > =A0 =A0print b =A0 =A0 =A0 =A0 =A0 =A0# 13
> > =A0 =A0print b +=3D ++b
> > =A0 =A0print b
>
> I didn't say I couldn't figure it out. =A0I said I had trouble figuring
it=
> out. =A0It is interesting that some awks produce 25 while others produce
> 28. =A0This is clearly some pretty tricky C coding that is not well
> understood by everybody.
Tricky? I'd rather call that "obfuscating coding" (if not "random
coding" or "unreliable coding"). Coding that way has not so much
to do with "understanding" such constructs. It's a well known class
of constructs that is called Side Effects. Any language that allows
constructs with a syntax that is "lexically ambiguous" has choices
to define the semantics in some way, or leave it explicitly as
"undefined behaviour". The C language definition has concepts of
sequencing points to explain behaviour of certain construct like
this one; though still not all lexically possible constructs are
covered, AFAIK.[*] It would be interesting to know whether awk
has similar rules or left the semantics in that case unspecified.
Janis
[partly off-topic...]
[*] Is evaluation of the following in the C standard defined in a
deterministic way...? { x=3Da; y=3Df(x++, ++x); }
C version:
#include<stdio.h>
int f(int p,int q) { return p*q; }
int main (void)
{ int x, y;
x=3D5; y=3Df(x++, ++x); printf ("%d\n", y);
x=3D5; y=3Df(++x, x++); printf ("%d\n", y);
return 0;
}
// Output:
36
35
Awk version (MKS):
function f(p,q) { return p*q }
BEGIN {
x=3D5; y=3Df(x++, ++x); printf ("%d\n", y);
x=3D5; y=3Df(++x, x++); printf ("%d\n", y);
}
# Output:
35
36


|