风声笔记

微信订阅号:fensnote 技术点滴,积少成多,水滴石穿,水到渠成……

导航

C语言输出颜色

命令后界面输出颜色

嵌入式终端界面输出日志时,为了区分输出的有用信息、错误信息,可以给不同级别的输出加上不同的颜色,以方便查看。
下面是颜色的定义:

//颜色宏定义
#define NONE         "\033[m"
#define RED          "\033[0;32;31m"
#define LIGHT_RED    "\033[1;31m"
#define GREEN        "\033[0;32;32m"
#define LIGHT_GREEN  "\033[1;32m"
#define BLUE         "\033[0;32;34m"
#define LIGHT_BLUE   "\033[1;34m"
#define DARY_GRAY    "\033[1;30m"
#define CYAN         "\033[0;36m"
#define LIGHT_CYAN   "\033[1;36m"
#define PURPLE       "\033[0;35m"
#define LIGHT_PURPLE "\033[1;35m"
#define BROWN        "\033[0;33m"
#define YELLOW       "\033[1;33m"
#define LIGHT_GRAY   "\033[0;37m"
#define WHITE        "\033[1;37m"

使用方法

如下:

printf(YELLOW"this print msg is yellow!\n"NONE);

这样打印出来的字符串: “this print msg is yellow!” 就是黄色的。
颜色值放在打印内容开始的双引号前面,结束时再双引号后面加上NONE,用于结束颜色作用域.

完成测试代码

#include <stdio.h>

//颜色宏定义
#define NONE         "\033[m"
#define RED          "\033[0;32;31m"
#define LIGHT_RED    "\033[1;31m"
#define GREEN        "\033[0;32;32m"
#define LIGHT_GREEN  "\033[1;32m"
#define BLUE         "\033[0;32;34m"
#define LIGHT_BLUE   "\033[1;34m"
#define DARY_GRAY    "\033[1;30m"
#define CYAN         "\033[0;36m"
#define LIGHT_CYAN   "\033[1;36m"
#define PURPLE       "\033[0;35m"
#define LIGHT_PURPLE "\033[1;35m"
#define BROWN        "\033[0;33m"
#define YELLOW       "\033[1;33m"
#define LIGHT_GRAY   "\033[0;37m"
#define WHITE        "\033[1;37m"


int main()
{
	printf(RED"红色****************************\n"NONE);
	printf(LIGHT_RED"亮红色**************************\n"NONE);
	printf(GREEN"绿色****************************\n"NONE);
	printf(LIGHT_GREEN"亮绿色****************************\n"NONE);

	printf(BLUE"蓝色******************************\n"NONE);
	printf(LIGHT_BLUE"亮蓝色****************************\n"NONE);

	printf(DARY_GRAY"灰色******************************\n"NONE);
	printf(CYAN"青色*****************************\n"NONE);
	printf(LIGHT_CYAN"亮青色****************************\n"NONE);
	
	printf(PURPLE"紫色*****************************\n"NONE);
	printf(LIGHT_PURPLE"亮紫色****************************\n"NONE);

	printf(BROWN"棕色*****************************\n"NONE);
	printf(YELLOW"黄色****************************\n"NONE);
	printf(LIGHT_GRAY"亮灰色*****************************\n"NONE);
	printf(WHITE"白色****************************\n"NONE);

	return 0;
}

运行结果

运行结果

posted on 2020-05-12 15:37  风声笔记  阅读(1992)  评论(0编辑  收藏  举报