Path: utzoo!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!usc!apple!tahoe!jimi!sonny-boy.cs.unlv.edu!grover From: grover@sonny-boy.cs.unlv.edu (Kevin Grover) Newsgroups: comp.lang.c Subject: Re: When do you use "if ( a = b )"? (was Re: Funny mistake) Message-ID: <1991Mar18.195351.11985@unlv.edu> Date: 18 Mar 91 19:53:51 GMT References: <8148@rsiatl.Dixie.Com> <15481@smoke.brl.mil> <775@camco.Celestial.COM> <65837@eerie.acsu.Buffalo.EDU> Sender: news@unlv.edu (News User) Reply-To: grover@sonny-boy.cs.unlv.edu (Kevin Grover) Organization: UNLV Computer Science and Electrical Engineering Lines: 45 In article <65837@eerie.acsu.Buffalo.EDU>, chu@acsu.buffalo.edu (john c chu) writes: ) From: chu@acsu.buffalo.edu (john c chu) ) Subject: When do you use "if ( a = b )"? (was Re: Funny mistake) ) Date: Sun, 17 Mar 91 04:53:11 PST ) Organization: SUNY Buffalo ) ) 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? ) There are plenty of uses for it. It will allow smaller code, and checks to be done quikcer. I have done it by mistake a time or two, but I find it quickly. One, of my most common uses is if ( !(fp=fopen("file","r")) ) perror ("Could not open file"); Which is the same (logically) as: if ( (fp=fopen("file","r")) == NULL ) perror ("Could not open file"); This code tries to open the file, if it did not, if prints a message (and aborts) the program. You could then put the rest of the code in an else (for clarity) or simply put it after the if. Also, if you get bitten but this bug often, try something like #define EQU == if ( a EQU b) Instead of depending upon remembering to use '==' when you need to. -- +-------------------------------------------------+----------------------+ | Kevin Grover UNLV Computer Science | Home of the | | grover@cs.unlv.edu Las Vegas, Nevada | Running REBELS | +-------------------------------------------------+----------------------+