Color text in C Programming

Started by
8 comments, last by anist 18 years, 7 months ago
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
Advertisement
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.
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
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.
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.
Check it out: [ http://www.codeblank.net/cpp/MyColor.h ]
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
Quote:Original post by Figherstarter
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


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)
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.
on XP the "color" command still works. so you can do this:
system("color 2");
system("cls");
printf("string in green");
As your leader, I encourage you from time to time, and always in a respectful manner, to question my logic. If you're unconvinced that a particular plan of action I've decided is the wisest, tell me so, but allow me to convince you and I promise you right here and now, no subject will ever be taboo. Except, of course, the subject that was just under discussion. The price you pay for bringing up either my Chinese or American heritage as a negative is - I collect your f***ing head.

This topic is closed to new replies.

Advertisement