Re: Preprocessor directive

From: John Bode (john_bode_at_my-deja.com)
Date: 10/03/03


Date: 3 Oct 2003 09:10:00 -0700

dam_fool_2003@yahoo.com wrote in message news:<a3e883de.0310030010.38ec36ba@posting.google.com>...
> Charles Harrison Caudill <kungfoo@myrna.cc.gatech.edu> wrote in message news:<blhm5o$7qp$2@solaria.cc.gatech.edu>...
> > Trying_Harder <frivolousdude@yahoo.com> wrote:
> > > Is it possible to redefine a macro with global scope after
> > > undefining it in a function? If yes, could someone explain
> > > how?
>
> > > /* This should print 15, but BLAH has a local scope */
> >
> > preprocessor directives don't have scope. The preprocessor will go from
> > the beginning of the program to the end and make replacements as necessary.
> > The first two BLAH's are being replaced by 10 because when test.h is
> > #included, it is #defined to 10. The BLAH in func1 was replaced by 15
> > because it was defined after main. If you put its declaration above main
> > then it will print 15 for all of them. Keep in mind, the preprocessor
> > is *not* operated at run-time, it is done *before* compilation.
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> What happens if macro is decleared in main().Just a doubt, that is all.

No, don't even think of them as being "declared" anywhere. Macros are
not variables. They don't act like variables. The preprocessor scans
the source file from top to bottom, replacing text as it goes.

#define BLAH 10
/* every occurence of BLAH from this point is replaced with 10 */
#undef BLAH
#define BLAH 20
/* every occurence of BLAH from this point is replaced with 20 */

It doesn't matter whether the preprocessor directive is embedded
within the text for a function definition or not. The preprocessor
doesn't understand what functions are, what scope is, or anything
else. All it sees is a stream of text.

There is no effective difference between:

    #define BLAH 10
    void f(void)
    {
        printf ("%d\n", BLAH);
    }

    void g(void)
    {
        printf ("%d\n", BLAH);
    }

and:

    void f(void)
    {
    #define BLAH 10
        printf ("%d\n", BLAH);
    }

    void g(void)
    {
        printf ("%d\n", BLAH);
    }

If you try something like:

    void f(void)
    {
        printf ("%d\n", BLAH);
    }

    #define BLAH 10
    int main (void)
    {
        f();
        return 0;
    }

the preprocessor will yak at you because BLAH hasn't been defined
before it's used in f(). The preprocessor goes straight from top to
bottom.
BLAH is not local to f() in the second example. It's not local to
anything.

Back to your example, before preprocessing:

> #define blaa 10
> void fun(void)
> {
> printf("%d\n",blaa);
> #ifdef blaa
> #undef blaa
> #define blaa 90
> #endif
> printf("now blaa is = %d\n",blaa);
> }
> int main(void)
> {
> fun();
> #ifdef blaa
> #undef blaa
> #define blaa 98
> #endif
> printf("now blaa has %d\n",blaa);
> return 0;
> }
>

After preprocessing:
> void fun(void)
> {
> printf("%d\n",10);
> printf("now blaa is = %d\n",90);
> }
> int main(void)
> {
> fun();
> printf("now blaa has %d\n",98);
> return 0;
> }



Relevant Pages

  • Re: #if in .rc file
    ... the "preprocessor" for .rc files did not exist. ... the C preprocessor functionality implemented as part of the resource compiler, ... #include "blah, blah" ... #include "yada, yada" ...
    (microsoft.public.vc.mfc)
  • Re: Preprocessor directive
    ... > preprocessor directives don't have scope. ... > the beginning of the program to the end and make replacements as necessary. ...
    (comp.lang.c)
  • Re: Preprocessor directive
    ... > Is it possible to redefine a macro with global scope after ... > undefining it in a function? ... preprocessor directives don't have scope. ... the beginning of the program to the end and make replacements as necessary. ...
    (comp.lang.c)
  • Re: enum while preprocessing
    ... but it seems that it (preprocessor) uses 0 unstead of BBB. ... Because converting unknown preprocessing identifiers to 0 is what it's ... "After all replacements due to ...
    (comp.lang.c)