code understanding help

Devon H. O'Dell dodell at offmyserver.com
Sun Apr 3 09:43:44 PDT 2005


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: pgp00003.pgp
Type: application/octet-stream
Size: 187 bytes
Desc: "Description: PGP signature"
URL: <http://lists.dragonflybsd.org/pipermail/users/attachments/20050403/4eade686/attachment-0021.obj>


More information about the Users mailing list