简体   繁体   中英

C preprocessor macro generation from another macro

I want to create a macro-list of macro from another macro-list of macro. I don't know how to describe this, so let me introduce an example :

#include <stdio.h>

#define     MACRO_LIST_DEFAULT              \
    MACRO(TEST1, TEST2)                     \
    MACRO(TEST3, TEST4)

#define     MACRO_LIST_MODIFIED             \
    MACRO(TEST1_MODIFIED, TEST2_MODIFIED)   \
    MACRO(TEST3_MODIFIED, TEST4_MODIFIED)

#define     MACRO_LIST_FULL                 \
    MACRO_LIST_DEFAULT                      \
    MACRO_LIST_MODIFIED

#define MACRO(VAR1, VAR2)   printf("%s:%s\n", #VAR1, #VAR2);

int main(void)
{
    MACRO_LIST_FULL
}

// OUTPUT :
// TEST1:TEST2
// TEST3:TEST4
// TEST1_MODIFIED:TEST2_MODIFIED
// TEST3_MODIFIED:TEST4_MODIFIED

This work perfectly, and it is what I want to do, but I don't want to fill the "MACRO_LIST_MODIFIED" by hand, but use the previous list "MACRO_LIST_DEFAULT"

something like that in fact :

#define MACRO_LIST_MODIFIED                 \
    MACRO_LIST_DEFAULT // And add _MODIFIED to elements.

I really don't know how to deal with this problem.

thank you in advance,

Here you go, I assume this is what you want to achieve and I assume you're using gcc:

#include <stdio.h>

#define     MACRO_LIST( x )              \
    MACRO(TEST1 ## x , TEST2 ## x )                     \
    MACRO(TEST3 ## x , TEST4 ## x )

#define     MACRO_LIST_FULL                 \
    MACRO_LIST( )                      \
    MACRO_LIST( _MODIFIED )

#define MACRO(VAR1, VAR2)   printf("%s:%s\n", #VAR1, #VAR2);

int main(void)
{
    MACRO_LIST_FULL
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM