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

Amateurgrammer eychaner at suncub.bbso.caltech.edu
Mon Mar 18 04:56:56 AEST 1991


chu at acsu.buffalo.edu (john c chu) writes:
>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?
Sure.  That's an easy one.  Example:

if (stat = some_func(...)) printf("Error %d: %s\n", stat, errstr(stat));

some_func(...) is a function which returns 0 if it completes successfully,
and an error code processed to s string by errstr(stat) if it fails.	
I know this isn't the greatest code, but you get the idea (I hope...).

******************************************************************************
Glenn Eychaner - Big Bear Solar Observatory - eychaner at suncub.bbso.caltech.edu
"Studies have shown that it is cheaper to build 
 things that don't work than things that do."      -Jeff Nenow, BBSO



More information about the Comp.lang.c mailing list