Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!ames!sun-barr!newstop!sun!sally!plocher From: plocher%sally@Sun.COM (John Plocher) Newsgroups: comp.lang.c Subject: Re: comma operator Message-ID: <118824@sun.Eng.Sun.COM> Date: 1 Aug 89 07:18:04 GMT References: <10099@mpx2.mpx.com> <93@microsoft.UUCP> <10100@mpx2.mpx.com> <10562@smoke.BRL.MIL> <1989Jul24.194646.3012@nc386.uucp> <5630@pt.cs.cmu.edu> <1989Jul28.174033.12734@jarvis.csri.toronto.edu> <918@helios.toronto.edu> Sender: news@sun.Eng.Sun.COM Reply-To: plocher@sun.UUCP (John Plocher) Organization: Sun Microsystems, Mountain View Lines: 70 In <918@helios.toronto.edu> dooley@helios.physics.utoronto.ca (Kevin Dooley) writes: >Can anybody tell me if there is a usage where the comma is 'better'. The only place I ever had a real use for the comma operator was when working with a 3rd party C-ISAM library which had many functions and a large set of error returns. Call the lib functions copen(), cclose(), cinsert(), cfind()... These routines would return a value X, where on success X > 0 and was then used as a magic cookie. If X < 0 then there were 15 or so error conditions that could have happened - all but 4 or 5 because of stupid programmer errors (The routines used structures that HAD to be initialized *just so* &$&%#$%# ). Output of all this is like: Trace: copen(structure, FLAG|FLAG, "Filename", blah) => DB_NOTFOUND (File not found) Trace: ccreate(structure,...) => DB_OK I actually used __LINE__ and __FILE__ in the ERROR() macro so the output was more useful still. -John Plocher ------------------------------------------------------------------------------------ #ifdef DEBUG int ERROR_CC; log(funct, string) char *funct, *string; { fprintf(stderr,"Trace: %s => %s\n", funct,string); } char *errorname( error_cc ) int error_cc; { if (error_cc > 0) return itoa(error_cc); else if (error_cc == DB_OK) return "DB_OK"; else if (error_cc == DB_NOTFOUND) return "DB_NOTFOUND (File not found)"; else if (error_cc == DB_BADSTRUCT)return "DB_BADSTRUCT (Invalid DB Struct)"; else if ... } /* Relies on NON-ANSI C Preprocessor behavior ! */ #define ERROR(funct) (log("funct", errorname(ERROR_CC=funct)), ERROR_CC) /* ** Call funct() and assign the ret val to ERROR_CC ** Send this value to errorname() so we get an english description of the error ** Send the (string representation) of the function and this english to log() ** Finally, give the poor user the ret value we saved in ERROR_CC */ #else /* not DEBUG */ #define ERROR(funct) funct #endif /* if DEBUG else endif */ main() { int dbfd; init( &structure ); dbfd = ERROR(copen(structure, FLAG|FLAG, "Filename", blah)); if (dbfd == DB_NOTFOUND) dbfd = ERROR(ccreate(structure,...)); if (dbfd < 0) bomb(); }