Path: utzoo!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!zaphod.mps.ohio-state.edu!rpi!uupsi!grebyn!ckp From: ckp@grebyn.com (Checkpoint Technologies) Newsgroups: comp.lang.c Subject: Re: When do you use "if ( a = b )"? (was Re: Funny mistake) Message-ID: <1991Mar17.175314.1484@grebyn.com> Date: 17 Mar 91 17:53:14 GMT References: <15481@smoke.brl.mil> <775@camco.Celestial.COM> <65837@eerie.acsu.Buffalo.EDU> Organization: Grebyn Timesharing Lines: 43 In article <65837@eerie.acsu.Buffalo.EDU> chu@acsu.buffalo.edu (john c chu) writes: >In article <775@camco.Celestial.COM> bill@camco.Celestial.COM (Bill Campbell) writes: >[concerning "if ( a = b )" >>Certainly it >>is a legal construction, but 90% of the time when I do this it >>was my mistake! > >It's been my mistake everytime I've done it!! I realize that it is a >legal construction and I know what it does, but I was wondering... >Is there a good use for this? No. More specifically, as long as it's really a simple assignment with no side effects, it's out. You *can* do this, and some might say it's a "valid" use: b = function(args); if(a = b) { ... Some programmer might have really meant it this way, to test the result from the function call for zero. But if I were writing it (and I hazard to guess most everyone else reading this would agree, at least in principle) it would appear this way: a = b = function(args); if(a) { .. /* or else if(b) */ I have seen the following code examples frequently, and I consider them valid: if(code = setjmp(jmploc)) { ... - and - if(pid = fork()) { ... The reason they are valid is because the expression contains a significant side effect. -- First comes the logo: C H E C K P O I N T T E C H N O L O G I E S / / ckp@grebyn.com \\ / / Then, the disclaimer: All expressed opinions are, indeed, opinions. \ / o Now for the witty part: I'm pink, therefore, I'm spam! \/