Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/xamarin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用C以红色打印变量的值_C_Printf - Fatal编程技术网

如何使用C以红色打印变量的值

如何使用C以红色打印变量的值,c,printf,C,Printf,我想用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_C

我想用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;
}
如何修复此错误

printf ("\033[31;1m Red dragon \033[0m\n");
这就是方法

另一个更好的方法是使用宏以ANSI方式执行

printf ("%s%s%s\n", ANSI_COLOR_RED, string, ANSI_COLOR_RESET);
另一种方法是

printf (ANSI_COLOR_RED "%s\n" ANSI_COLOR_RESET, string);
这就是方法

另一个更好的方法是使用宏以ANSI方式执行

printf ("%s%s%s\n", ANSI_COLOR_RED, string, ANSI_COLOR_RESET);
另一种方法是

printf (ANSI_COLOR_RED "%s\n" ANSI_COLOR_RESET, string);

或者,更好的是,
printf(“%s%s%s\n”,ANSI\u COLOR\u RED,string,ANSI\u COLOR\u RESET)@AlexP.:是的……甚至更好。甚至
printf(ANSI\u COLOR\u RED“%s\n”ANSI\u COLOR\u RESET,字符串)
:)
@DavidC.Rankin:我现在不能信任我的网络,似乎…切换端口需要编辑时间…这是在我的终端上完成的。谢谢……)我总是这样。或者,更好的是,
printf(“%s%s%s\n”,ANSI\u COLOR\u RED,string,ANSI\u COLOR\u RESET)@AlexP.:是的……甚至更好。甚至
printf(ANSI\u COLOR\u RED“%s\n”ANSI\u COLOR\u RESET,字符串)
:)
@DavidC.Rankin:我现在不能信任我的网络,似乎…切换端口需要编辑时间…这是在我的终端上完成的。谢谢……)这种情况经常发生在我身上。你可以连接字符串文字(如答案中的选项3所示),但你可以连接文字和变量,就像你尝试做的那样。你可以连接字符串文字(如答案中的选项3所示),但你可以连接文字和变量,就像你尝试做的那样。