Path: utzoo!news-server.csri.toronto.edu!rpi!uwm.edu!lll-winken!taco!mcnc!duke!drh From: drh@duke.cs.duke.edu (D. Richard Hipp) Newsgroups: comp.lang.c Subject: Re: When do you use "if ( a = b )"? (was Re: Funny mistake) Message-ID: <669305909@juliet.cs.duke.edu> Date: 18 Mar 91 14:18:30 GMT References: <15481@smoke.brl.mil> <775@camco.Celestial.COM> <65837@eerie.acsu.Buffalo.EDU> Organization: Duke University Computer Science Dept.; Durham, N.C. Lines: 23 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? Here is something I do a lot, to remove the "\n" from the end of a string (something that is commonly necessary if "fgets" is used to read lines): if( cp=strchr(buf,'\n') ) *cp = 0; Actually, I usually write this slightly different, to avoid complaints from lint about suspicious assignments used as booleans: if( (cp=strchr(buf,'\n'))!=0 ) *cp = 0; The same code gets generated either way.