一个有关于C宏命令中 ##__VA_ARGS__ 的使用问题

#define TAG_LYK_TEST "this_class_LykTest"
#define LOGEM_LYK_TEST(format,...) \
printf(format, TAG_LYK_TEST": Function:%s, :Line:%d\n", ##__VA_ARGS__, \
FUNCTION, LINE)

#include
int main(){
char s[] = "abcd";
LOGEM_LYK_TEST("printf a string :%s", s);

return 0;

}

这段代码打印出的结果是:
printf a string :this_class_LykTest: Function:%s, :Line:%d

哪位好心的大佬给分析分析,这里的%s和%d为什么没有被__Function__和__LINE__替换掉????

为什么要用__Function__和__LINE__替换,你可以在利用一个%s和%d

#define LOGEM_LYK_TEST(format,...)  \
        printf(format, TAG_LYK_TEST ": Function:%s, :Line:%d\n", ##__VA_ARGS__, __FUNCTION__, __LINE__)

=====>这里的printf(format后面是不需要逗号","的。去掉该逗号,如下:

#define LOGEM_LYK_TEST(format,...)  \
        printf(format TAG_LYK_TEST ": Function:%s, :Line:%d\n", ##__VA_ARGS__, __FUNCTION__, __LINE__)  //去掉format后面的逗号,则可。

若有帮助,望采纳!!!谢谢。。。。。

下次可以直接贴代码格式,看起来更方便

#include <stdio.h>

#define TAG_LYK_TEST "this_class_LykTest"
#define LOGEM_LYK_TEST(format,...) \
    printf(format, TAG_LYK_TEST": Function:%s, :Line:%d\n", ##__VA_ARGS__, \
         __func__, __LINE__)

int main(){
    char s[] = "abcd";
    LOGEM_LYK_TEST("printf a string :%s", s);
    return 0;
}

如果在format后加逗号的话,那么其实格式串只是

printf a string :%s

而后面的两个字符串,会被认为是这个%s的匹配,也就成了

printf("printf a string:%s","this_class_LykTest"": Function:%s, :Line:%d\n",##__VA_ARGS__,__func__,__LINE__)

因为格式串里只有一个%s占位符,那也只会打印出后面一个字符串。想要的__func__,__LINE__自然不会被打印出来。

去掉format后的逗号就好了