如何以跨平台的方式在控制台上打印彩色? [英] How can I print to the console in color in a cross-platform manner?

查看:93
本文介绍了如何以跨平台的方式在控制台上打印彩色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Mac OS X和Linux上使用printf输出彩色文本?

How can I output colored text using "printf" on both Mac OS X and Linux?

推荐答案

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.

这篇关于如何以跨平台的方式在控制台上打印彩色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆