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?
-
This SO question can you give you more information. You should check the color codes for different colors. Also be careful to reset to defaults. You have to just format the output logs with different color codes. [Link 1](http://stackoverflow.com/questions/2353430/how-can-i-print-to-the-console-in-color-on-mac-os-x-in-a-cross-platform-manner). [Link 2](http://stackoverflow.com/questions/3219393/stdlib-and-colored-output-in-c) – Praveen S Jul 18 '10 at 10:26
5 Answers
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!

- 1,695
- 1
- 19
- 15
-
This answer is good for Linux and Mac, though won't work for Windows, as the op asked. Well, unless you had loaded the ansi.sys module on Windows xp boot (won't work on Windows Vista and above). – Baltasarq Feb 27 '15 at 15:45
-
@Baltasarq, thank you for the notice! I updated my comment so it fits Windows OS also! – Ron Feb 27 '15 at 17:03
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.
-
Well, in a way the Windows API is also an external library, just a pretty huge one. And Terminal escape sequences are an API of their own, just a very awkward to use ;-) – Joey Jul 18 '10 at 08:41
-
1
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

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

- 5,984
- 2
- 38
- 55
-
-
if you use the codes in Link1 postet by Praveen on a standard sh (not bash, not csh, not zsh) [sorry, i meant to write Unix instead of Linux in my answer] you get what you write, the "control codes", not what you want, "the colors". – Peter Miehle Jul 18 '10 at 12:18
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)

- 12,014
- 3
- 38
- 57