Before you printf something print this escape sequence to produce it in the desired color. This program asks the user to play with attributes and colors and shows a string in that color. I usually use it to find out the best combination of colors for my GUIs. Text version of above program is here . As we know, there are many terminals with very few capabilities and terminals which don't recognize these escape codes or need different codes to achieve the same effect.
Curses uses terminfo to find the correct codes to accomplish the task in style.
Terminfo is a big database which contains information about the various functionalities of different terminals. But if you just want to write a simple program which produces color on a Linux console or xterm window, you can just use the escape sequences above to do it easily. The Linux console mostly emulates vt100, so it recognizes these escape sequences. But there is a way to query the terminfo database and do the work. The two capabilities setf and setb are useful to set foreground and background colors.