C语言 C 中的 stdlib 和彩色输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3219393/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
stdlib and colored output in C
提问by Andrejs Cainikovs
I am making a simple application which requires colored output. How can I make my output colored like emacs and bash do?
我正在制作一个需要彩色输出的简单应用程序。如何使我的输出像 emacs 和 bash 一样着色?
I don't care about Windows, as my application is only for UNIX systems.
我不关心 Windows,因为我的应用程序仅适用于 UNIX 系统。
回答by Andrejs Cainikovs
All modern terminal emulators use ANSI escape codes to show colours and other things.
Don't bother with libraries, the code is really simple.
所有现代终端模拟器都使用 ANSI 转义码来显示颜色和其他内容。
不要打扰库,代码非常简单。
More info is here.
更多信息在这里。
Example in 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 (int argc, char const *argv[]) {
printf(ANSI_COLOR_RED "This text is RED!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_GREEN "This text is GREEN!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_YELLOW "This text is YELLOW!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_BLUE "This text is BLUE!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_MAGENTA "This text is MAGENTA!" ANSI_COLOR_RESET "\n");
printf(ANSI_COLOR_CYAN "This text is CYAN!" ANSI_COLOR_RESET "\n");
return 0;
}
回答by Andrejs Cainikovs
Dealing with colour sequences can get messy and different systems might use different Colour Sequence Indicators.
处理颜色序列可能会变得混乱,不同的系统可能使用不同的颜色序列指示器。
I would suggest you try using ncurses. Other than colour, ncurses can do many other neat things with console UI.
我建议您尝试使用ncurses。除了颜色之外,ncurses 还可以使用控制台 UI 做许多其他整洁的事情。
回答by Stephen
You can output special color control codes to get colored terminal output, here's a good resource on how to print colors.
您可以输出特殊的颜色控制代码以获得彩色终端输出,这里有一个关于如何打印颜色的好资源。
For example:
例如:
printf("3[22;34mHello, world!3[0m"); // shows a blue hello world
EDIT: My original one used prompt color codes, which doesn't work :( This one does (I tested it).
编辑:我原来的一个使用提示颜色代码,这不起作用:(这个可以(我测试过)。
回答by Praveen S
You can assign one color to every functionality to make it more useful.
您可以为每个功能分配一种颜色,以使其更有用。
#define Color_Red "[0:31m\]" // Color Start
#define Color_end "[0m\]" // To flush out prev settings
#define LOG_RED(X) printf("%s %s %s",Color_Red,X,Color_end)
foo()
{
LOG_RED("This is in Red Color");
}
Like wise you can select different color codes and make this more generic.
同样,您可以选择不同的颜色代码并使之更通用。
回答by alhelal
If you use same color for whole program , you can define printf()function.
如果整个程序使用相同的颜色,则可以定义printf()函数。
#include<stdio.h>
#define ah_red "\e[31m"
#define printf(X) printf(ah_red "%s",X);
#int main()
{
printf("Bangladesh");
printf("\n");
return 0;
}
回答by Ladon
#include <stdio.h>
#define BLUE(string) "\x1b[34m" string "\x1b[0m"
#define RED(string) "\x1b[31m" string "\x1b[0m"
int main(void)
{
printf("this is " RED("red") "!\n");
// a somewhat more complex ...
printf("this is " BLUE("%s") "!\n","blue");
return 0;
}
reading Wikipedia:
阅读维基百科:
- \x1b[0mresets all attributes
- \x1b[31msets foreground color to red
- \x1b[44mwould set the background to blue.
- both : \x1b[31;44m
- both but inversed : \x1b[31;44;7m
- remember to reset afterwards \x1b[0m...
- \x1b[0m重置所有属性
- \x1b[31m设置前景色为红色
- \x1b[44m会将背景设置为蓝色。
- 两者:\x1b[31;44m
- 两者相反:\x1b[31;44;7m
- 记得之后重置\x1b[0m...
回答by baz
Because you can't print a character with string formating. You can also think of adding a format with something like this
因为您无法使用字符串格式打印字符。您还可以考虑添加类似这样的格式
#define PRINTC(c,f,s) printf ("3[%dm" f "3[0m", 30 + c, s)
fis format as in printf
f格式如下 printf
PRINTC (4, "%s\n", "bar")
will print blue bar
将打印 blue bar
PRINTC (1, "%d", 'a')
will print red 97
将打印 red 97

