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

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

问题描述

如何在 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!
", 27); // red
  printf("%c[1;32mHello, world!
", 27); // green
  printf("%c[1;33mHello, world!
", 27); // yellow
  printf("%c[1;34mHello, world!
", 27); // blue
  return 0;
}

27escape 字符.如果您愿意,可以使用 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天全站免登陆