且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何以跨平台的方式在控制台上打印彩色?

更新时间:2023-12-04 10:56:40

ANSI颜色代码。下面是一个示例程序:

You can use the ANSI colour codes. Here's an example program:

#include <stdio.h>

int main(int argc, char *argv[])
{
  printf("%c[1;31mHello, world!\n", 27); // red
  printf("%c[1;32mHello, world!\n", 27); // green
  printf("%c[1;33mHello, world!\n", 27); // yellow
  printf("%c[1;34mHello, world!\n", 27); // blue
  return 0;
}

27 escape 字符。您可以使用 \e

The 27 is the escape character. You can use \e if you prefer.

网络上有所有代码的列表。 这里是一个

There are lists of all the codes all over the web. Here is one.