cc-preprocessorstringification

How to contain escape sequences in macro definition #?


I am trying to use this macro definition in C:

#define STR(x) #x

Is it possible to contain some escape sequences inside x? e.g., I want to define a string like:

char* str = "\'";

This declaration does not seem to work:

char* str1 = STR(\');

If I must use this macro definition, is there some way to contain strings containing apostrophe "\'"? Thanks!


Solution

  • const char* img = "/Image/'.png";
    

    can also be written as

    const char* img = "/Image/" "'" ".png";
    

    so you could use

    #define IMG(x) "/Image/" x ".png"
    
    const char* img = IMG("'");