When do you use "if ( a = b )"? (was Re: Funny mistake)

Chris Torek torek at elf.ee.lbl.gov
Fri Mar 22 17:25:12 AEST 1991


[I wrote: `if your compiler generates different code for embedded
assignments than for separate assignments, that compiler is not worth
what you paid for it.']

In article <dxj+VD at ppcger.ppc.sub.org> Sepp at ppcger.ppc.sub.org (Josef Wolf)
writes:
>You might be right in this point of view, but...
[and goes on to argue that embedded assignment tends to be clearer.]

I happen to agree---most often, a loop like:

	while ((c = getchar()) != EOF) {
		do some work;
	}

is MUCH better, stylistically, than

	c = getchar();
	while (c != EOF) {
		do some work;
		c = getchar();
	}

(Never mind translating into `for' loop syntax :-) )

>Well, the above is _my_ opinion. You have to find _your_ way to get
>around this problem.

Do not make the mistake of thinking that every argument I make is a
direct clue to my own opinions.  I could, for instance, show how VMS
is better than <pick some version here> Unix at some particular task.
(Such tasks do exist, especially with older Unixes.)  Nonetheless, I
have managed to avoid VMS systems for many years now, and plan to
continue avoiding them.

In this particular case, I would be quite happy with a compiler that
warns about

	if (a = expr) ...

but not about

	if ((a = expr) != 0) ...

and I tend to write the latter when I mean `assign, then test the result'.
-- 
In-Real-Life: Chris Torek, Lawrence Berkeley Lab CSE/EE (+1 415 486 5427)
Berkeley, CA		Domain:	torek at ee.lbl.gov



More information about the Comp.lang.c mailing list