Color console in ANSI C?

2020-02-06 06:55发布

Is it possible to color the console output in just plain ANSI C? Without an external library? Can this be done in Windows, Linux, or Mac OS X?

标签: c console colors
5条回答
▲ chillily
2楼-- · 2020-02-06 07:36

It is true that ISO C knows nothing about the console being capable of displaying colors, however there is an ANSI norm for console capabilities management, based on escape character controls. This works transparently in Linux and Mac OS X, however it fails in Windows, in which you need to use the primitives of the Win32 API.

You can find below a very simple library that allows to clear the screen, show colors and locate the cursor in a specific coordinate, in a multiplatform way (Win32 & Unix-like systems).

It comes with plain C source files (.c and .h), doxygen documentation in Spanish (doc/), and a simple demo (main.c)

http://github.com/Baltasarq/cscrutil/

查看更多
够拽才男人
3楼-- · 2020-02-06 07:42

Yes, in Linux/ Mac it is possible using ANSI C89. You can either manipulate the font and the color of the text. using the following command:

printf("%c[0;00mHello, world!\n", 27); /* White color     */
printf("%c[1;33mHello, world!\n", 27); /* Yellowish color */
printf("%c[1;34mHello, world!\n", 27); /* Blueish color   */

Notice that the left part of the ";" (where the numbers 0, 1 are) manipulates the text font, the right part of ";" manipulates the colors. You can experiment on your own and find out new colors.

This code compiles using "-ansi -pedantic" command with no warnings nor errors.

***** Edit ***** In Windows based systems you can achieve colorful console text/background of text using the following example:

#include <stdio.h>
#include <windows.h>

int main(void)
{   
    /* Point to our console */
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    int i = 0;

    /* Iterate through colors */ 
    for(; i < 255; i++)
    { /* i stands for color type: could refer to actual text color or background color of text */
        SetConsoleTextAttribute(hConsole, i);
        printf("Colorful text");
    }

    getchar();
    return 0;
}

Good luck!

查看更多
ら.Afraid
4楼-- · 2020-02-06 07:44

in Linux this can be done, if you you know the shell-specific control codes / Escape sequences.

查看更多
叼着烟拽天下
5楼-- · 2020-02-06 07:45

Linux/OSX/Unix

On posix systems you can use the ANSI escape sequences.

Windows

On windows it is a bit more complicated, there are multiple solutions:

Win32 API

Using the Win32 API to set the output color before printing to the console using SetConsoleTextAttribute and friends. This is a lot more cumbersome than simply embedding ANSI escape sequences in your strings, and requires you to handle Windows as a special case.

Windows ANSI.SYS and Replacement

Older version of windows contained ANSI.SYS, but this has been removed in later versions. ANSICON is a replacement for this that you can install to get ANSI color code support in the windows command prompt: https://github.com/adoxa/ansicon

Embeddable no external dependencies solution

Here is a project that can be easily integrated into any existing project without relying on ANSI.SYS or ANSICON being installed.

It takes a string containing ANSI escape sequences and translates them to the relevant Win32 equivalent API functions: https://github.com/mattn/ansicolor-w32.c

查看更多
祖国的老花朵
6楼-- · 2020-02-06 07:52

just plain ANSI C?

No. The C standard doesn't assume the stdout is a console or has color.

Can this be done in Windows, Linux, or Mac OS X?

Yes. See How can I print to the console in color on Mac OS X in a cross-platform manner? for Linux and Mac OS X.

For Windows, you may need to directly access the Console Functions if you want to avoid external libraries.

查看更多
登录 后发表回答