Is this correct action for the c compiler/preprocessor ??
der Mouse
mouse at mcgill-vision.UUCP
Sun Nov 3 16:09:04 AEST 1985
> 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
More information about the Comp.lang.c
mailing list