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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T17:57:16+00:00 2026-06-13T17:57:16+00:00

Possible Duplicate: Convert a preprocessor token to a string #define num 1234 I want

  • 0

Possible Duplicate:
Convert a preprocessor token to a string

#define num 1234

I want to define a “const char*” based on num, in the sample it would be:

#define num_str "1234"

Can I write a macro statement to achieve this?
NB: 1234 would be changed.

Thanks.

  • 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-06-13T17:57:17+00:00Added an answer on June 13, 2026 at 5:57 pm

    Yes you can, but the macro substitution can get a little strange-looking. The double-macro substitution is there for a reason, and if you think about it for awhile, it will become clear why it is needed.

    #define STRINGIZER_(exp)   #exp
    #define STRINGIZER(exp)    STRINGIZER_(exp)
    #define NUM 1234
    
    int main(int argc, char *argv[])
    {
        const char *p = STRINGIZER(NUM);
        printf("%s\n",p);
        return EXIT_SUCCESS;
    }
    

    Running this:

    1234
    

    The reason for the double substitution: At first glance one may think this will solve the problem:

    #define STRINGIZER(exp)    #exp
    #define NUM 1234
    
    int main(int argc, char *argv[])
    {
        const char *p = STRINGIZER(NUM);
        printf("%s\n",p);
        return EXIT_SUCCESS;
    }
    

    But this produces:

    NUM
    

    which is not what we’re trying to do. If you want the actual expansion of the NUM macro first then that is what you have to do: force the expansion. By forcing the preprocessor to substitute first through an intermediate expansion (as I show at the top of this answer) the passed-in macro is expanded first, then string-ized.

    Side Bar: This technique is particularly useful for generating wide-char versions of predefined preprocessor macros that otherwise hold regular strings. For example, the __FILE__ macro. Suppose you wanted a wide-char version of this (a string prepended with ‘L’) You may first think this will work:

    #define WIDESTR(str)    L##str
    

    but expanding this with __FILE__ as in:

    const wchar *p = WIDESTR(__FILE__);
    

    will result in a compiler error: "Undefined identifier: L__FILE__"

    So how can we address this? The same way we did above.

    #define WIDESTR_(str)       L##str
    #define WIDESTR(str)        WIDESTR_(str)
    
    int main(int argc, char *argv[])
    {
        const wchar_t* p = WIDESTR(__FILE__);
        wcout << p << endl;
        return EXIT_SUCCESS;
    }
    

    On my system, this produces:

    /Users/craig/tmp/main/main/test.cpp
    

    In Closing…

    As a consolation prize, we combine everything in this answer into one giant goo-pile, what do we suppose happens when we do this:

    int main()
    {
        const wchar_t *p = WIDESTR(STRINGIZE(NUM));
        wcout << p << endl;
        return EXIST_SUCCESS;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Convert std::string to const char* or char* is there any to get
Possible Duplicate: Convert std::string to const char* or char* Simple question but I'm new
Possible Duplicate: Convert std::string to const char* or char* void setVersion(char* buf, std::string version)
Possible Duplicate: Convert std::string to const char* or char* I have a std::string and
Possible Duplicate: Convert String to another locale in java I want to convert a
Possible Duplicate: Convert a number to the shortest possible character string while retaining uniqueness
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How can I convert a string to boolean in JavaScript? I have
Possible Duplicate: How can I convert String to Int? public List<int> GetListIntKey(int keys) {
Possible Duplicate: convert string to 2D array using php I have the string like

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.