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 6563291
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:48:46+00:00 2026-05-25T13:48:46+00:00

I want to compile code conditionally based on a macro. Basically I have a

  • 0

I want to compile code conditionally based on a macro. Basically I have a macro that looks like (Simplified from the real version):

#if DEBUG
    #define START_BLOCK( x ) if(DebugVar(#x) \
        { char debugBuf[8192];
    #define END_BLOCK( ) printf("%s\n", debugBuf); }
#else
    #define START_BLOCK( x ) (void)0;
    #define END_BLOCK( ) (void)0;
#endif

The issue is that if DEBUG is defined you could do things like:

START_BLOCK( test )
     char str[] = "Test is defined";
     strcpy(debugBuf, str);
END_BLOCK( )

START_BLOCK( foo )
    char str[] = "Foo is defined";
    strcpy(debugBuf, str);
END_BLOCK( )

And everything works fine because each block is within it’s own scope. However if DEBUG isn’t defined, then you’d get a redefinition of str in the second block. (Well you’d also get debugBuf not defined but that’s just a side effect of the simplified example.)

What I’d like to do is to have the #else be something like:

#else
    #define START_BLOCK( x ) #if 0
    #define END_BLOCK( ) #endif
#endif

Or some other method of not having anything between the start / end blocks be compiled. I tried the above, I also tried something along the lines of:

#else
    #define NULLMACRO( ... ) (void)0
    #define START_BLOCK( x ) NULLMACRO(
    #define END_BLOCK( ) )
#endif

without any luck.

Is there a way for this to work? One thought that just occurred to me is that I could maybe abuse the optimizing compiler and use:

#else
    #define START_BLOCK( x ) if(0){
    #define END_BLOCK( ) }
#endif

And trust that it will just compile out the block completely. Are there any other solutions?

  • 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-25T13:48:47+00:00Added an answer on May 25, 2026 at 1:48 pm

    So you want conditional blocks with their own scope?

    Here’s a quite readable solution that relies on the compiler to optimize it away:

    #define DEBUG 1
    
    if (DEBUG) {
        // ...
    }
    

    And here is one that is preprocessor-only:

    #define DEBUG 1
    
    #ifdef DEBUG
        #define IFDEBUG(x) {x}
    #else
        #define IFDEBUG(x)
    #endif
    
    IFDEBUG(
        // ...
    )
    

    Or manually:

    #define DEBUG 1
    
    #ifdef DEBUG
    {
        // ...
    }
    #endif
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to compile C code from the Command Prompt in Windows. I have
I have code that I want to compile on all unix systems, but if
I have difficulty in using the vb code dom. Basically, I want to compile
I want to compile the following line of code from http://code.google.com/p/enhsim : enh::eout <<
In C++ if we do not want some statements to compile into code that
I want to write code that compiles conditionally and according to the following two
I want to compile some code that targets .NET v3.5 in Visual Studio 2005.
I'm making a game engine for mobile devices. I want to compile my code,
The following code does not want to compile. See the included error message. Code:
I want to compile GCC and binutils which would produce 64bit executables. From GNU

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.