Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!sdd.hp.com!spool.mu.edu!cs.umn.edu!talon.UCS.ORST.EDU!usenet!ogicse!intelhf!ichips!iwarp.intel.com!inews!hopi!bhoughto From: bhoughto@hopi.intel.com (Blair P. Houghton) Newsgroups: comp.lang.c Subject: Re: When do you use "if ( a = b )"? (was Re: Funny mistake) Message-ID: <3482@inews.intel.com> Date: 27 Mar 91 05:07:07 GMT Article-I.D.: inews.3482 References: <1991Mar18.195351.11985@unlv.edu> <11109@dog.ee.lbl.gov> <15053@ganymede.inmos.co.uk> Sender: news@inews.intel.com Organization: Intel Corp, Chandler, AZ Lines: 54 In article <15053@ganymede.inmos.co.uk> conor@inmos.co.uk (Conor O'Neill) writes: >To me it is clear that using assignments inside conditions is very bad >style because it makes the intention of the original programmer unclear. No, it's bad because it's an unnecessary obfuscation. No compiler in the world won't make a=b if(a) the same as if(a=b) as far as any user can tell (provided codocil 4 of para 16 of the charter of the League of Nations is followed and you didn't declare a as volatile or apply the -g flag...) >The only sort of situation which I can think of where I _might_ consider >using it is something like the following: > >if ( (x != 0) && ((a = expensive_function(x)) != 0) ) > { > ... do something involving 'a' > } >else if ... more complicated tests Well, the one I always fret over is if ( rindex(path,'/') ) /* dammit; another pathname */ strcat(newpath, 1+rindex(path,'/')); else /* bitchin'; just a filename */ strcat(newpath, path); Yeah, I can declare another variable, but golly, bytes are soooo expensive... (No strrchr-flames, please; I don't do this where I have an ANSI library). > 5 if (a) b = 99; > 7 if (a != 0) b = 99; > 8 if (a = b) b = 99; /* This is the confusing one */ > 9 if ((a = b) != 0) b = 99; >lion/test(49)% icc c.c >Warning-icc-c.c(8)- use of '=' in condition context >lion/test(50)% Nice stuff. Did you try if ( (a==b) != 0 ) and get a redundancy error? --Blair "Some of us are redundant. Others merely overloaded."