LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-21-2010, 04:35 PM   #1
Dogs
Member
 
Registered: Aug 2009
Location: Houston
Distribution: Slackware 13.37 x64
Posts: 105

Rep: Reputation: 25
Who knew about printf() in C being able to specify foreground/background colors?


I got curious during the construction of my program to search for blocks of text that contain certain words, and wanted to print out blocks of text with the words I was searching for, initially, in ALL CAPS and seperated by tabs for easy identification. That got boring, though, so I thought, "Can printf do colors, too?"

Yes it can.... Who knew?

Code:
/*Text attributes
       0    All attributes off
       1    Bold on
       4    Underscore (on monochrome display adapter only)
       5    Blink on
       7    Reverse video on
       8    Concealed on

    Foreground colors
       30    Black
       31    Red
       32    Green
       33    Yellow
       34    Blue
       35    Magenta
       36    Cyan
       37    White

    Background colors
       40    Black
       41    Red
       42    Green
       43    Yellow
       44    Blue
       45    Magenta
       46    Cyan
       47    White
*/

Code:
	printf("%c[%d;%dmHello World%c[%dm\n",27,1,33,27,0);
 
Old 11-21-2010, 05:30 PM   #2
Dark_Helmet
Senior Member
 
Registered: Jan 2003
Posts: 2,786

Rep: Reputation: 374Reputation: 374Reputation: 374Reputation: 374
Well, I don't think that's printf() doing the color changing. Those are escape sequences interpreted by the terminal. So, what happens with that string of characters depends on what features the terminal supports.
 
Old 11-21-2010, 06:49 PM   #3
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Yup - that's probably old DOS code that uses "ansi.sys". The giveaway was the "<esc>[" ANSI driver character sequence (esc == ASCII "27").

Like the song goes "As primitive as can be..."

You can read more about ansi/vt100 terminal control sequences here:
http://www.termsys.demon.co.uk/vtansi.htm

And who knows - it might even work in any random xterm window or command prompt you might try it on. Go for it!

Last edited by paulsm4; 11-21-2010 at 06:51 PM.
 
Old 11-21-2010, 11:30 PM   #4
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by paulsm4 View Post
...
And who knows - it might even work in any random xterm window or command prompt you might try it on. Go for it!
Why not ? http://en.wikipedia.org/wiki/ANSI_terminal
 
Old 11-22-2010, 12:42 AM   #5
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Q: Why not?
A: Because it's EQUALLY likely to NOT work in any random xterm window or command prompt you might try. There are arguably much better ways to accomplish the same thing.

IMHO...
 
Old 11-22-2010, 04:03 AM   #6
JohnGraham
Member
 
Registered: Oct 2009
Posts: 467

Rep: Reputation: 139Reputation: 139
Quote:
Originally Posted by paulsm4 View Post
There are arguably much better ways to accomplish the same thing.
Just out of interest, what are they? It's just I've never heard of any way of colouring text without using aforementioned escape sequences.
 
Old 11-22-2010, 04:54 AM   #7
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
Interesting ... not that useful, but interesting. You can try it out in awk as well, it has compatible syntax.
 
Old 11-22-2010, 11:17 AM   #8
orgcandman
Member
 
Registered: May 2002
Location: new hampshire
Distribution: Fedora, RHEL
Posts: 600

Rep: Reputation: 110Reputation: 110
Quote:
Originally Posted by JohnGraham View Post
Just out of interest, what are they? It's just I've never heard of any way of colouring text without using aforementioned escape sequences.
For *nix systems, http://tldp.org/HOWTO/NCURSES-Progra...WTO/color.html

I believe that ncurses interprets the TERM variable, and references the terminfo definition from the termcap library. Since I don't use ncurses, I could be completely wrong on this, however.
 
Old 11-22-2010, 11:32 PM   #9
Dogs
Member
 
Registered: Aug 2009
Location: Houston
Distribution: Slackware 13.37 x64
Posts: 105

Original Poster
Rep: Reputation: 25
Fun topic. I'll give ncurses a try after this semester is done.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I can't set my xterm background/foreground colors properly using Slackware. joeBuffer Linux - Software 1 08-19-2009 09:45 AM
background process popping up in foreground noir911 Linux - General 1 12-14-2007 11:53 PM
Background and Foreground Datamike Linux - Software 2 01-20-2006 04:20 PM
How to change backgound/foreground colors in eclipse wearetheborg Linux - Software 0 09-13-2004 01:56 PM
How do I customize background/foreground colors in Eclipse? wearetheborg Linux - Newbie 0 09-10-2004 02:48 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:13 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration