code understanding help
Terry Tree
terry.tree at gmail.com
Sun Apr 3 10:40:53 PDT 2005
On Apr 3, 2005 4:40 PM, Devon H. O'Dell <dodell at xxxxxxxxxxxxxxx> wrote:
> On Sun, Apr 03, 2005 at 04:36:48PM +0000, Terry Tree wrote:
> > I'm trying to go through a book on programming in C and I'm having
> > problem understanding the second example in the book.
> >
> > $cat 2.c
> >
> > #define PRINTX printf("%d\n", x);
> >
> > int
> > main(int argc, char *argv[])
> > {
> > int x = 2, y, z;
> >
> > x *= 3 + 2; PRINTX;
>
> *= means multiply by right hand side and assign to left hand
> side. Thus:
>
> x = x * (3 + 2);
> x = 2 * (5);
> x = 10;
>
> > x *= y = z = 4; PRINTX; // the output makes no sense
>
> Again, multiply and assign. First evaluate the right hand side:
>
> x *= (y = z = 4)
> (y and z are assigned to 4; 4 stays on the stack)
>
> x = x * 4;
> x = 10 * 4;
> x = 40;
>
> > x = y == z; PRINTX;
> > x == (y = z); PRINTX;
> > }
> >
> > Looking at line x *= y = z = 4; from my point of view the output
> > should be 8 but it is 40.
>
> What book is this? This PRINTX macro abuse is horrible.
>
> --Devon
>
Thanks for the help. I was thinking that x was being set back to 2
for the second line.
The book is the C puzzle book. The reviews on amazon said it was one
of the best books for learning C programming.
More information about the Users
mailing list