Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/5/84; site bunker.UUCP Path: utzoo!watmath!clyde!burl!ulysses!allegra!mit-eddie!genrad!decvax!ittvax!bunker!garys From: garys@bunker.UUCP (Gary M. Samuelson) Newsgroups: net.lang.c Subject: Re: Re: condition convention 'if (10 == j)...' Message-ID: <822@bunker.UUCP> Date: Tue, 30-Apr-85 09:48:38 EDT Article-I.D.: bunker.822 Posted: Tue Apr 30 09:48:38 1985 Date-Received: Thu, 2-May-85 00:47:35 EDT References: <137@bocklin.UUCP> <> <332@cubsvax.UUCP> Organization: Bunker Ramo, Trumbull Ct Lines: 34 > > >> 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