Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7527891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T04:13:26+00:00 2026-05-30T04:13:26+00:00

Is there a way to protect a macro definition? To be more specific consider

  • 0

Is there a way to protect a macro definition? To be more specific consider the following:

#define macro1 x

// code segment 1 involving macro1 goes here 

// code segment 2 involving macro1 goes here

// code segment 3 involving macro1 goes here

In the example I have placed 3 comments denoting code segments involving the macro. What I would like to do now is to be able to avoid having the macro affect code segment 2. Is there a way to tell the compiler to replace all instances of macro1 in segment 1 and segment 3 but not in segment 2? Here is a possible way:

#define macro1 x

// code segment 1 involving macro1 goes here 

#undef macro1

// code segment 2 involving macro1 goes here

#define macro1 x

// code segment 3 involving macro1 goes here

The drawback is that I have to define the macro again. Say I wanted to use the word
NULL in my program (don’t ask me why just go with it). I want this to be a variable
but the C preprocessor will change it to 0 in most cases. So what I want to do, is to be
able to block it for just a short time period and then let it be what it was before.

Failed Attempt:

Let us suppose that macro1 has been defined somewhere externally and we do not even know what the value of such macro is. All we want is to avoid having it replace things in the second segment.

// code segment 1 involving macro1 goes here 

#ifdef macro1
#define TEMP macro1
#undef macro1
#endif

// code segment 2 involving macro1 goes here

#ifdef TEMP
#define macro1 TEMP
#undef TEMP
#endif

// code segment 3 involving macro1 goes here

The idea is to check if the macro exists, if it does, then we want to store the value into another variable, undefine the macro and finally define the macro again when we need it.
Unfortunately, this code doesn’t work because before we execute code segment 3 macro1 will be replaced by TEMP, not the value that TEMP was supposed to have.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T04:13:28+00:00Added an answer on May 30, 2026 at 4:13 am

    The easiest way to have a chunk of code not be impacted by a macro is to put the macro in a header .h file and NOT include the header file in the source file that is getting compiled.

    This means you might have to “chop up” a bigger file to get your section of code that shouldn’t be impacted by the macro out. Yes, it is a pain, but it sounds like it shouldn’t be with the rest of the code anyway (or it wouldn’t be an issue).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to password protect SAS CODE within SAS? Or, if not,
I know that there is no way to fully protect our code. I also
Is there a way to run a project inside a solution that has more
I encountered the following macro definition when reading the globals.h in the Google V8
Is there any way to generate project docs during automated builds? I'd like to
My SQL is a bit rusty -- is there a SQL way to project
Is there a way you can run your UnitTest project against a compiled DLL
Is there any way to run ASDoc on your project via the Flex Builder
Is there a way to access the Project object from a NAnt extension function,
Is there any way to write a Visual Studio Project file easy or do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.