tags:

views:

76

answers:

4

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?

+1  A: 

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.

KennyTM
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
A: 

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

Peter Miehle
Those doesn't have anything to do with the shell.
Joey
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
A: 

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://trevinca.ei.uvigo.es/~jgarcia/FP/lib/scrutil.zip

Baltasarq
A: 

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. Link 2

Praveen S