Concatenate int to string using C Preprocessor

前端 未结 3 1226
离开以前
离开以前 2020-11-28 22:25

I\'m trying to figure out how I can concatenate a #define\'d int to a #define\'d string using the C Preprocessor. My compiler is

3条回答
  •  北荒
    北荒 (楼主)
    2020-11-28 23:05

    Classical C preprocessor question....

    #define STR_HELPER(x) #x
    #define STR(x) STR_HELPER(x)
    
    #define MAJOR_VER 2
    #define MINOR_VER 6
    #define MY_FILE "/home/user/.myapp" STR(MAJOR_VER) STR(MINOR_VER)
    

    The extra level of indirection will allow the preprocessor to expand the macros before they are converted to strings.

提交回复
热议问题