DragonFly BSD
DragonFly users List (threaded) for 2005-04
[Date Prev][Date Next]  [Thread Prev][Thread Next]  [Date Index][Thread Index]

Re: code understanding help


From: "Devon H. O'Dell " <dodell@xxxxxxxxxxxxxxx>
Date: Sun, 3 Apr 2005 18:40:54 +0200
Mail-followup-to: users@crater.dragonflybsd.org

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

Attachment: pgp00003.pgp
Description: PGP signature



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