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

D. Richard Hipp drh at duke.cs.duke.edu
Tue Mar 19 00:18:30 AEST 1991


In article <65837 at eerie.acsu.Buffalo.EDU> chu at acsu.buffalo.edu (john c chu) writes:
>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?

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.



More information about the Comp.lang.c mailing list