How to define macro function which support no input parameter and support also input parametr in the same time

前端 未结 4 1746
不知归路
不知归路 2021-01-15 22:12

I want to define a macro function which support at the same time:

1) No input parameter

2) Input parameters

some thing like that:

#de         


        
4条回答
  •  春和景丽
    2021-01-15 22:32

    Not exactly that but ...

    #include 
    
    #define MTEST_
    #define MTEST__(x) printf("%d\n",x)
    #define MACRO_TEST(x)\
        printf("this is a test\n");\
        MTEST_##x    
    
    int main(void)
    {
        MACRO_TEST();
        MACRO_TEST(_(5));
        return 0;
    }
    

    EDIT

    And if 0 can be used as skip:

    #include 
    
    #define MACRO_TEST(x) \
        do { \
            printf("this is a test\n"); \
            if (x +0) printf("%d\n", x +0); \
        } while(0)
    
    int main(void)
    {
        MACRO_TEST();
        MACRO_TEST(5);
        return 0;
    }
    

提交回复
热议问题