Link to home
Start Free TrialLog in
Avatar of slavikn
slavikn

asked on

printf - can I print in color?

Hi,

When I use "printf", the printed text is silver.
Can I print in other colors aswell? (even basic 16 colors are enough).

Thanks!
Avatar of Jaime Olivares
Jaime Olivares
Flag of Peru image

Not with standard C function, but may be possible.
That will depend of the compiler and library you are using.
Please specify compiler/environment.

Avatar of slavikn
slavikn

ASKER

UNIX. g++. (only ANSI C is allowed)
By the way, sorry that I don't give more points - I just don't have any :-(
ASKER CERTIFIED SOLUTION
Avatar of ankuratvb
ankuratvb
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
If you are working in Linux with terminal emulation you can use ANSI escape sequences but you have to ensure you terminal type is ANSI or VT100, will not work with any terminal model emulated.
Here is a complete guide: http://www.graphcomp.com/info/specs/ansi_col.html
Avatar of slavikn

ASKER

Thanks, ankuratvb.

To jaime_olivares, I am working in Unix, rather than Linux with terminal emulation.
Also, check out the TERMCAP (You are probably using TERMINFO these days) entries for the terminals in UNIX - you will find many control codes in those files for your terminals... the files are used to make a standard control code interface for the different terminals that are connected to your UNIX box - mapping defined constants to terminal escape sequences.  Your shell program you are using to log in and any program that has to communticate with the terminal uses them.  You use "curses" functions to get the control codes from the aprropriate terminal file and use them to draw boxes and move the cursor around the screen - if it is a color terminal you are using, it should have control codes defined to change colors in those files.  I remember a few years back, I used curses functions to write a recursive menu system for an MIS project.

The TERMCAP or TERMINFO are stored in the /etc directory.  The difference between TERMCAP and TERMINFO is that TERMCAP is the older version and is just a text files - TERMINFO is newer, and if I remember correctly, it is actually compiled somehow - it has been a couple of years since I have had to use it.  Check out the man pages on TERMINFO and see if it tells you how to view the files... there should be an entry defined for your terminal - just go to the /etc directory and work from there.

If you have any questions, send me an email.
Oh, one other thing...

Provided you are using a color terminal, or a PC with your TERMCAP or TERMINFO shell variable set to a color terminal type... that is how you specify your terminal type to the shell in UNIX - just set that variable appropriately, check out the TERMCAP or TERMINFO files under the /etc directory and find out the escape sequences for your PC or terminal that will change the color as desired ... and just print the escape sequence to change the color out first with printf and then print your text, then you can change the color back the same way.

printf( "%sThis is a test%s\n", "\(whatever)\...", "\(whatever)\..." ); -  get it?

The only problem with this approach is that if someone trys to run your program under a different terminal type, he would probably just see garbage as the escape sequences are printed, becuase they are different with different terminals.  To do it right, you should really query the TERMINFO file using the curses functions and get the correct escape sequences by just giving the control code to the curses function - it will then look at the TERMINFO shell variable and look in the right TERMINFO terminal file to get the right escape sequence - you would then use that escape sequence in your printf - (you would have to call the curses function twice though, once to get the espcape code to change the color, and once to get the one to change it back).

And if I was not to clear, the /etc/TERMCAP and the /etc/TERMINFO files are actually directories - which have files that are named after the terminals in them.  You  probably are using TERMINFO - if you can not read the files with the vi editor, make sure you check out the man pages as to how to uncompile them so that you can view them - I can not remember the command (it has been a few years since I had to mess with it) - but do not change the files at all - unless you know what your doing and know that you are changing them correctly - you would only change escape sequence codes in the TERMCAP or TERMINFO files if they were defined wrong or you needed to add a new terminal type that was not defined previously.

I hope this helps you - send me an email if you have further problems.