Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.1 6/24/83; site mcgill-vision.UUCP Path: utzoo!linus!philabs!micomvax!musocs!mcgill-vision!mouse From: mouse@mcgill-vision.UUCP (der Mouse) Newsgroups: net.lang.c Subject: Re: Is this correct action for the c compiler/preprocessor ?? Message-ID: <326@mcgill-vision.UUCP> Date: Sun, 3-Nov-85 01:09:04 EST Article-I.D.: mcgill-v.326 Posted: Sun Nov 3 01:09:04 1985 Date-Received: Tue, 5-Nov-85 07:54:59 EST References: <8510301206.AA27124@ucbvax.berkeley.edu> Organization: McGill University, Montreal Lines: 72 > Consider the folowing short program. > #define BAD_SEGMENT 29 > #define ERROR(number) printf("Error number %d\n",number); > main() { > ERROR(BAD_SEGMENT); > } > what should this program do ???? This is open to interpretation. All current preprocessors I know of substitute "number" inside the double quotes as well. Of course, this is not serious; merely rewrite ERROR as #define ERROR(n) printf("Error number %d\n",n); PS. There really should be no semicolon after the definition; as it stands the result of expanding ERROR(BAD_SEGMENT); is printf("Error BAD_SEGMENT %d\n",BAD_SEGMENT);; which makes a difference if it is the then clause of an if statement without braces (yes, Virginia, there are people who write that sort of statement).) This seems to be a common stumbling block. There seems to be confusion between *expanding macros* inside a quoted string and *replacing macro formals* inside a quoted string. The compiler designers originally chose (perhaps not conciously) to replace formals inside strings, which is in fact useful for debugging: #define LOGINTEGER(i) printf("i = %d\n",i) which not only prints out the value desired but prints the expression producing it, as in LOGINTEGER(table[index]); which expands into printf("table[index] = %d\n",table[index]); a nice feature. Of course, the statement printf("LOGINTEGER(foo)\n"); will not be touched. Macros are not expanded inside double quotes; but inside double quotes in the replacement string, formals get changed to actuals. This actually has more frequent use in a definition like #define CTRLCHAR(c) ('c'&0x1f) (used on ASCII machines). Here, it is necessary that c be substituted inside the (single) quotes, so for uniformity if nothing else the same should happen with double quotes. > Is this correct ??? Apparently it is de-facto correct. Anyone with a copy of the ANSI C standard want to answer this? -- der Mouse {ihnp4,decvax,akgua,etc}!utcsri!mcgill-vision!mouse philabs!micomvax!musocs!mcgill-vision!mouse Hacker: One responsible for destroying / Wizard: One responsible for recovering it afterward