Newsgroups: comp.lang.c
Path: utzoo!henry
From: henry@utzoo.uucp (Henry Spencer)
Subject: Re: evaluation order
Message-ID: <1989Sep14.162316.26287@utzoo.uucp>
Organization: U of Toronto Zoology
References: <9361@attctc.Dallas.TX.US>
Date: Thu, 14 Sep 89 16:23:16 GMT

In article <9361@attctc.Dallas.TX.US> bobc@attctc.Dallas.TX.US (Bob Calbridge) writes:
>if (write(handle, buf, len=strlen(buf)) != len) do_something();
>
>can I be assured that 'len' will be assigned the length of 'buf' before it
>is used on the right side of the comparison operator...

There is a sequence point at the function call, but it doesn't save you
because you don't know which operand of != gets evaluated first.  In
general this sort of thing isn't very safe.  Better would be:

	len=strlen(buf);
	if (write(handle, buf, len) != len) do_something();

Is there some reason why you can't just write it that way?

As in APL, C "one-liners" are a perverse art form, suitable for presentation
to the Obfuscated C Contest but usually inappropriate in production code.
-- 
V7 /bin/mail source: 554 lines.|     Henry Spencer at U of Toronto Zoology
1989 X.400 specs: 2200+ pages. | uunet!attcan!utzoo!henry henry@zoo.toronto.edu
