printf打印出不同颜色的输出内容

当打希望不同类型的打印显示不同的颜色,比如错误显示红色, 正常打印显示绿色。

#include <stdio.h>

#define PRINT_NONE(fmt,args...)   ({printf("\033[m info: %s(%d) %s: ",__FILE__, __LINE__, __func__);printf(fmt"\r\n" ,##args);})
#define PRINT_RED(fmt,args...)   ({printf("\033[0;32;31m info: %s(%d) %s: ",__FILE__, __LINE__, __func__);printf(fmt"\r\n" ,##args);})
#define PRINT_GREEN(fmt,args...)   ({printf("\033[0;32;32m info: %s(%d) %s: ",__FILE__, __LINE__, __func__);printf(fmt"\r\n" ,##args);})
#define PRINT_BLUE(fmt,args...)   ({printf("\033[0;32;34m info: %s(%d) %s: ",__FILE__, __LINE__, __func__);printf(fmt"\r\n" ,##args);})
#define PRINT_CYAN(fmt,args...)   ({printf("\033[0;36m info: %s(%d) %s: ",__FILE__, __LINE__, __func__);printf(fmt"\r\n" ,##args);})
#define PRINT_YELLOW(fmt,args...)   ({printf("\033[1;33m info: %s(%d) %s: ",__FILE__, __LINE__, __func__);printf(fmt"\r\n" ,##args);})
#define PRINT_WHITE(fmt,args...)   ({printf("\033[1;37m info: %s(%d) %s: ",__FILE__, __LINE__, __func__);printf(fmt"\r\n" ,##args);})
#define PRINT_BROWN(fmt,args...)   ({printf("\033[0;33m info: %s(%d) %s: ",__FILE__, __LINE__, __func__);printf(fmt"\r\n" ,##args);}) 

#define PRINT_PARTIAL(fmt,args...)   ({printf("\033[0;32;31m info:\033[m %s(%d) %s: ",__FILE__, __LINE__, __func__);printf(fmt"\r\n" ,##args);})

 int main()
{
    PRINT_NONE("hello !!!\n");
    PRINT_RED("hello !!!\n");
    PRINT_GREEN("hello !!!\n");
    PRINT_BLUE("hello !!!\n");
    PRINT_CYAN("hello !!!\n");
    PRINT_YELLOW("hello !!!\n");
    PRINT_WHITE("hello !!!\n");
    PRINT_BROWN("hello !!!\n");


    PRINT_PARTIAL("hello !!!\n");


    return 0;
}

输出结果:

猜你喜欢

转载自blog.csdn.net/weixin_59665492/article/details/120142668