condition convention 'if (10 == j)...'

Gary M. Samuelson garys at bunker.UUCP
Tue Apr 30 23:48:38 AEST 1985


> 
> >> To prevent silly mistakes like 
> >> 	if (j = 10)
> >> I usually write
> >> 	if (10 == j)
> >> By putting the constant first, I ensure that the compiler will catch the 
> >> typo.

> Just that instead of writing, in the traditional manner,
> 	if(pc=malloc(nbytes)==NULL)...
> you have to insert extra parens if you use the other form:
> 	if(NULL==(pc=malloc(nbytes)))..

> This is because == has higher precedence than =,

Note that the above statement is true no matter what order is used.

> and the expression will first evaluate NULL==pc,
> then try to set the resulting constant to whatever 
> malloc() returns (except, of course, the compiler doesn't
> let you get that far).

So your example will compare malloc()'s return value with NULL,
and then assign either 'true' or 'false' to pc.  Not too good.

> I used to use the (NULL==var form but reverted to the more traditional
> form because the extra parens kept making me do a double-take.
> I'd rather skip the parens and always put the constant at the end.

I'd rather put in the parens and get correct results.

> Peter S. Shenkin,  Biology, Columbia Univ.   cubsvax!peters

Gary Samuelson



More information about the Comp.lang.c mailing list