CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    What is the scope of #undef ?

    I don't know whether I should be proud or ashamed to say that I've never in my whole life needed to use #undef - until today. but what is the 'scope' of a #undef statement (i.e. how long does it last for, if it appears in a source file, as distinct from a header file?)

    Let's say I'm compiling a project with twenty cpp source files. I have a preprocessor definition - let's say, #define MY_DEFINE "Hello World" which I've set up in my IDE.

    In module 5 (out of the twenty modules) I need to #undef MY_DEFINE for some reason. Is it then #undef'd until the compiler finishes compiling or is it only #undef'd for that one source file?

    In other words, when the compiler reaches the 6th source file, will it re-establish #define MY_DEFINE "Hello World"
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    Join Date
    Dec 2004
    Location
    Poland
    Posts
    1,165

    Re: What is the scope of #undef ?

    Remeber that compiler processes each translation unit separately. If you undef some defined sybol, and afterwards compile another file with the same compiler settings, symbol will be defined again.
    Each define works from place of occurence to the end of translation unit. The same is for undef, unless symbol is redefined.
    Anyway, playing with macros is risky, and playing this way seems to be more than risky
    B+!
    'There is no cat' - A. Einstein

    Use [code] [/code] tags!

    Did YOU share your photo with us at CG Members photo gallery ?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured