When do you use "if ( a = b )"? (was Re: Funny mistake)

Trent Tobler ttobler at unislc.uucp
Thu Mar 21 05:11:03 AEST 1991


>From article <65837 at eerie.acsu.Buffalo.EDU>, by chu at acsu.buffalo.edu (john c chu):
> In article <775 at camco.Celestial.COM> bill at 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?

Yes.  I often use it to do the following...

---------------------
if( fp = fopen( fname, "r")) {
	do_stuff_to(fp);
	fclose(fp);
}
else
	perror( fname);
----------------------

Of course, it could be written with fp = fopen ...  on a line by itself,
followed by an if(fp) ... ,  but I find the above easier to follow.


--
  Trent Tobler  - ttobler at csulx.weber.edu , ttobler at unislc!sun!uucp



More information about the Comp.lang.c mailing list