• 9
• 9
• 11
• 12
• 9

# Color text in C Programming

This topic is 4610 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I am working on a Pong game and have the majority of it done, now I am just trying to make it look all nice and neat. Is there any way I can use color in my printf()? I read somewhere that cprintf() can be used but I am not quite to sure how this differs from printf. Also what arguments do I use to make the text color change? Any simple example would do. I was able to change the background color of the console using system("color 16") Thank you very much. Gil

##### Share on other sites
Standard C has no functions to control color in output text. However, there may be libraries for your operating system that can output colored text.

##### Share on other sites
Any library that anyone has come across would be helpful. I am a novice at libraries but I can get the configured. A web page or any information would be helpful. If not, thanks anyway. By the way is the Quake III source code incredible hard to understand. Are there any source codes from novice first person shooters. I would like to see it, that would be VERY cool.

Gil

##### Share on other sites
You see, this is the point where you're supposed to mention what operating system you're using so we can point you toward some libraries that you can use.

##### Share on other sites
This should print the string 'foo!!' in red. Hope this helps!

printf( "\033[31;1mfoo!!\033[m\n" );

This works by embedding ANSI terminal escape sequences into the output stream.. Most console/terminal apps understand these codes and will do the colors. Just google for ansi color codes and that should give you more info.

##### Share on other sites
Check it out: [ http://www.codeblank.net/cpp/MyColor.h ]

##### Share on other sites
Thank you much, I am going to try this method out. By the way, I use Windows XP. But I would probably prefer not to use OS specific libraries since other OS's would not take advantage of the color. Thanks for the info guys.

Gil

##### Share on other sites
Quote:
 Original post by FigherstarterThank you much, I am going to try this method out. By the way, I use Windows XP. But I would probably prefer not to use OS specific libraries since other OS's would not take advantage of the color. Thanks for the info guys.Gil

As SiCrane said, the language has no concept of color, or even of printing text to a console window.
It simply puts the string you supply it with into the stream that's connected to standard out, and hopes that someone on the receiving end will know what to do with it. The OS will then print the text on the screen, but technically, your program doesn't know that. It just supplies text to a stream.

So if you want to actually format the output, you have to use some kind of OS-specific library. (I believe that for Windows, you can include conio.h to get color as well as other formatting options)

##### Share on other sites
I'm not sure if the ANSI escape sequences will work on XP. Even back on Windows 98 you had to add ansi.sys to the config.sys file to get it to work. As of WindowsME it wouldn't work. It can't hurt to try it.

Windows provides a console library that will handle color output and that sort of stuff. In the *nix systems you can use curses.