【发布时间】:2020-09-22 09:18:21
【问题描述】:
我试图让用户输入一种颜色,输出将是所选颜色的句子 我正在使用设置为变量的 ansi 转义码,我不确定如何使用最后一个
#define red "\033[1;31m"
#define green "\033[0;32m"
#define blue "\033[0;34m"
#define magenta "\033[0;35m"
#define yellow "\033[0;33m"
#define cyan "\033[0;36m"
int main()
{
char colour1[10];
printf("enter a colour");
scanf("%s", colour1);
printf("%s this is test\n ", red);
printf("%s another test \n", green);
printf("%s hello test ", colour1);
return 0;
}
所以说如果用户输入“蓝色”,它只会输出单词 blue 而不是 color , 谢谢你 任何帮助将不胜感激
【问题讨论】:
-
C 中的宏是编译时字符串替换,因此不会影响运行时。您应该创建一个表和一个代码来查找该表。
-
如果用户输入
garble,printf("%s hello test ", colour1)将打印garble hello test。现在如果用户输入blue,为什么printf("%s hello test ", colour1)打印的不是blue hello test?
标签: c colors header macros ansi