C 中的 stdlib 和彩色输出 [英] stdlib and colored output in C

查看:38
本文介绍了C 中的 stdlib 和彩色输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making a simple application which requires colored output. How can I make my output colored like emacs and bash do?

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

I don't care about Windows, as my application is only for UNIX systems.

推荐答案

所有现代终端模拟器都使用 ANSI 转义码来显示颜色和其他内容.
不用担心库,代码很简单.

All modern terminal emulators use ANSI escape codes to show colours and other things.
Don't bother with libraries, the code is really simple.

更多信息在这里.

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 "
");
  printf(ANSI_COLOR_GREEN   "This text is GREEN!"   ANSI_COLOR_RESET "
");
  printf(ANSI_COLOR_YELLOW  "This text is YELLOW!"  ANSI_COLOR_RESET "
");
  printf(ANSI_COLOR_BLUE    "This text is BLUE!"    ANSI_COLOR_RESET "
");
  printf(ANSI_COLOR_MAGENTA "This text is MAGENTA!" ANSI_COLOR_RESET "
");
  printf(ANSI_COLOR_CYAN    "This text is CYAN!"    ANSI_COLOR_RESET "
");

  return 0;
}

这篇关于C 中的 stdlib 和彩色输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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