#include <stdio.h>
int main()
{
printf("\033[0;30Hello world!\n");
printf("\033[0;31Hello world!\n");
printf("\033[0;32Hello world!\n");
printf("\033[0;33Hello world!\n");
printf("\033[0;34Hello world!\n");
printf("\033[0;35Hello world!\n");
printf("\033[0;36Hello world!\n");
printf("\033[0;37Hello world!\n");
}
我为发布这些内容感到as愧,但是我对C完全一无所知,当我尝试使用loop进行操作时,它总是会引发一些错误。
我尝试过这样的事情:
for(int i=30; i<=37; i++){
char* color = "\033[0;"+itoa(i);
printf("%s Hello world", color);
}
好吧,大量的错误,例如我试图用int或somethink连接字符串。我还尝试了strcat,进行类型转换,将所有内容都赋予printf并设置%d而不是变量和其他一些东西。我正在开发虚拟机,而且真的很辛苦,需要您的帮助,因此我急需它。
顺便说一句:还有比30-37多的颜色吗?我知道您可以将其设为1而不是0等的粗体。
你要这个:
for(int i=30; i<=37; i++){
char color[20];
snprintf(color, sizeof(color), "\033[%dm", i);
printf("%s Hello world\n", color);
}
甚至更简单,但是先前的代码更具可读性
for(int i=30; i<=37; i++){
printf("\033[%dmHello world\n", i);
}
您尝试中有几个问题:
for(int i=30; i<=37; i++){
char* color = "\033[0;"+itoa(i); << - you can't add strings with '+'
- itoa requires 3 parameters
- you forgot the 'm' at the end of the
- escape sequence
printf("%s Hello world", color);
}
未经测试的代码,但它至少应该给您一个想法。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句