Path: utzoo!mnetor!uunet!husc6!hao!ames!oliveb!sun!betelgeuse!jamesa From: jamesa%betelgeuse@Sun.COM (James D. Allen) Newsgroups: comp.lang.c Subject: Re: comma operator Message-ID: <39121@sun.uucp> Date: 17 Jan 88 18:20:53 GMT References: <3819@sigi.Colorado.EDU> Sender: news@sun.uucp Lines: 42 Summary: A real example The comma operator is one of the things that makes C what it is. For example while (A, B) is equivalent to for (A; B; A) and generates less code on most compilers. The latter may be considered more readable, but that seems like evidence of a mental block. Apropos of "comma in macro definition" here are some excerpts from "real code". The program is used to solve the "Logic Puzzles" in Dell Crosswords magazine although it could be adapted for related problems. Logic propositions are represented, converted directly into compiled C, and all solutions are found (through backtracking). Logic propositions can be nested arbitrarily. 'AND' is implied by concatenation, 'OR' requires an "EITHER...OR...OR...OR...END_EITHER" as in the example: /* * Clue 3) Tom and Janice were on their honeymoon but one of them * spent the whole time eating. */ MRS_AND_MR(JANICE, TOM) EITHER IS_TRUE(TOM, EAT) OR IS_TRUE(JANICE, EAT) END_EITHER Here are some of the relevant "#defines": #define EITHER if (Psol[1] = Psol[0], ! setjmp((++Psol)->jb)) { #define OR } else EITHER #define TRY_AGAIN longjmp((Psol--)->jb, 1) #define END_EITHER } else TRY_AGAIN; The interesting macro is "EITHER". It copies and pushes a large structure representing the problem state: "Psol[1] = Psol[0], ++Psol", and sets a checkpoint for later backtracking: "setjmp()". I didn't see a way to do this without the "," operator. (avoid variable number of "}" in END_EITHER). If there's interest I'll post the program.