Re: Is this C?



Duncan Muirhead wrote:
I came across code somewhat like this, and was somewhat
puzzled by it. Is it C, or does gcc just compile it (" gcc -Wall -std=c89 -pedantic odd.c eg.c -o odd")
without complaints?
The idea is to override a function by defining a new function with an extra argument, and a macro.
(The reason was that in the real code the function
being replaced was called many times, and the aim
was to gather more diagnostic information in debugging
without having to change all the calls).

eg.c:
#include <stdio.h>
void printit( int it)
{ printf( "It: %d\n", it);
}

odd.c:
#include <stdlib.h>
#include <stdio.h>
void new_printit( int it, char* who)
{ printf( "%s: It's %d\n", who, it);
}
#define WHO "main"
#define printit( i) new_printit( i, WHO)
#include "eg.h"
int main( int argc, char** argv)
{ printit( argc);
(printit)( argc);
return EXIT_SUCCESS;
}

eg.h:
void (printit)( int it);

The bits that I find puzzling are:
a. "(printit)" in eg.h. Without the brackets, of course, the
printit macro is invoked and syntax errors arise. b. "(printit)" in odd.c. This means that the printit() from
eg.c gets called, not the macro.

So it looks as if the preprocessor does not replace (printit)
with (<bodyofprintit>). Is this C? Is this only because the
printit macro takes parameters?

Yes and yes. The invocation of a function-like macro
consists of the macro name followed by a parenthesis-enclosed
list of its arguments (possibly empty). The name in any other
context is not a macro invocation, and the preprocessor just
leaves it alone. Hence

printit(argc) -- invokes the printit macro and expands
it to new_printit(argc, WHO)

(printit)(argc) -- not a macro invocation, no expansion

"printit(argc)" -- not a macro invocation

printit /**/ (argc) -- invokes and expands the printit
macro, because comments and white space don't matter

--
Eric Sosman
esosman@xxxxxxxxxxxxxxxxxxxx
.



Relevant Pages

  • Wheres the beef?
    ... Is it possible to define a macro substitution in the C++ preprocessor so ... heavily invested in MFC, and certain capabilities (Class Wizard & Browser ... no longer usable under previous development systems. ...
    (microsoft.public.vsnet.ide)
  • Re: tolower macro
    ... tolower is a macro defined like this. ... between a lower case character and it's uppercase equivalent. ... Does the preprocessor just ... replacing the character sequences given inside the macro as the names ...
    (comp.lang.c.moderated)
  • Re: Mapping source instructions to machine instructions
    ... reality as well) operates in two separate phases. ... This isn't really much different from a C compiler and its preprocessor. ... m4 macro processor before feeding the output to the assembler proper. ... Anyway, in many, perhaps most, assemblers the macro processor is very ...
    (alt.lang.asm)
  • tolower macro
    ... tolower is a macro defined like this. ... Does the preprocessor just ... more like an inline function? ... have an appropriate newsgroups line in your header for your mail to be seen, ...
    (comp.lang.c.moderated)
  • Re: Anonymous functions in C.
    ... Steve Thompson wrote: ... prevent side-effects from occuring when variables are expanded in macros, ... them unless they happen to be macro identifiers or preprocessor ... pre-processor could not automatically protect macro arguments at very ...
    (comp.lang.c)