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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:48:49+00:00 2026-05-14T14:48:49+00:00

I have some fairly generic code which uses preprocessor macros to add a certain

  • 0

I have some fairly generic code which uses preprocessor macros to add a certain prefix onto other macros. This is a much simplified example of what happens:

#define MY_VAR(x) prefix_##x

“prefix_” is actually defined elsewhere, so it will be different each time the file is included. It works well, but now I have some code I would like to skip if one of the tokens doesn’t exist, but this doesn’t work:

#if defined MY_VAR(hello)

What I want it to expand to is this:

#ifdef prefix_hello

But I can’t figure out how. I need to use the MY_VAR() macro to do the expansion, so I can’t just hardcode the name. (It’s actually for some testing code, the same code gets included with a different prefix each time to test a bunch of classes, and I want to skip a couple of tests for a handful of the classes.)

Is this possible with the C++ preprocessor?

Update:

Here is some semi-compilable code to demonstrate the problem further: (to avoid squishing it into the comments below)

#define PREFIX hello

#define DO_COMBINE(p, x)  p ## _ ## x
#define COMBINE(p, x)     DO_COMBINE(p, x)
#define MY_VAR(x)         COMBINE(PREFIX, x)

// MY_VAR(test) should evaluate to hello_test

#define hello_test "blah blah"

// This doesn't work
#ifdef MY_VAR(test)
  printf("%s\n", MY_VAR(test));
#endif
  • 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-14T14:48:50+00:00Added an answer on May 14, 2026 at 2:48 pm

    Is there more to your program than this question describes? The directive

    #define MY_VAR(x) prefix_##x
    

    defines exactly one preprocessor identifier. The call

    blah ^&* blah MY_VAR(hello) bleh <>? bleh
    

    simply goes in one end of the preprocessor and comes out the other without defining anything.

    Without some other magic happening, you can’t expect the preprocessor to remember what arguments have been passed into what macros over the course of the preceding source code.

    You can do something like

    #define prefix_test 1
    #if MY_VAR(test) == 1
    #undef prefix_test // optional, "be clean"
    ...
    #endif
    #undef prefix_test
    

    to query whether the prefix currently has the particular value prefix_. (Undefined identifiers are replaced with zero.)

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

Sidebar

Related Questions

I have a function that runs some fairly generic code that does a lot
We have some fairly old code which uploads, resizes and then saves an image
We have a couple of sites which have some memory problems and fairly often
I have bunch of strings, some of which are fairly long, like so: movie.titles
I have some fairly straightforward code to open up files using a Process object:
I have some fairly complex libraries that interconnect with each other to do some
I have some fairly complex routing rules, that are only achievable with custom code,
I have some Scala code that makes fairly heavy use of generics, and I
I have a fairly basic question regarding python lists/dictionaries which I would like some
I'm doing some fairly simple cross-platform TCP socket programming. I have unfortunately found out

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.