我想用C打印彩色文本,这是我的代码:
#include <stdio.h>
#define ANSI_COLOR_RED "\x1b[31m"
#define ANSI_COLOR_GREEN "\x1b[32m"
#define ANSI_COLOR_YELLOW "\x1b[33m"
#define ANSI_COLOR_BLUE "\x1b[34m"
#define ANSI_COLOR_MAGENTA "\x1b[35m"
#define ANSI_COLOR_CYAN "\x1b[36m"
#define ANSI_COLOR_RESET "\x1b[0m"
int main()
{
char *string = "Test";
printf("%s", ANSI_COLOR_RED string ANSI_COLOR_RESET);
return 0;
}在编译时,输出一个错误:
game.c:18:35: error: expected ‘)’ before ‘string’
printf("%s", ANSI_COLOR_RED string ANSI_COLOR_RESET);如何修复此错误?
发布于 2017-11-11 06:13:09
printf ("\033[31;1m Red dragon \033[0m\n");这就是方法。
另一种更好的方法是使用宏实现ANSI-way。
printf ("%s%s%s\n", ANSI_COLOR_RED, string, ANSI_COLOR_RESET);另一种方法就是
printf (ANSI_COLOR_RED "%s\n" ANSI_COLOR_RESET, string);https://stackoverflow.com/questions/47234970
复制相似问题