c - C 中的标准库和彩色输出

标签 c colors stdio libc

我正在制作一个需要彩色输出的简单应用程序。如何使我的输出像 emacs 和 bash 一样着色?

我不关心 Windows,因为我的应用程序仅适用于 UNIX 系统。

最佳答案

所有现代终端仿真器都使用 ANSI 转义码来显示颜色和其他内容。
不用理会库,代码真的很简单。

更多信息是 here .

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;
}

关于c - C 中的标准库和彩色输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3219393/

相关文章:

colors - MeshLab:如何导入 XYZRGB 文件

ios - Swift - 如何绘制一条线,其中每个点都可以有单独的颜色

c - 为什么 scanf 会陷入无效输入的无限循环?

c - 保证唯一整数组合的 LUT 集

c - 字符串变量和 echo (C) *作业*

c++ - size_t 是否仅在 C++ 标准或 C 标准中?

c - EEPROM 数据传输至 TeraTerminal

python - 某些seaborn绘图颜色未在投影仪上显示

c - 这样创建的文件放在哪里?

c - 多个 `FILE` 附加到同一文件