Thread: Strings with colors

  1. #1
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50

    Strings with colors

    hi
    This is my first post.everybody know that printf() prints the screen. is their any way
    that I can give color full out put using printf(). I mean is it possible to output colored
    text to the dos prompt.
    thanks.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    There's no standard way.
    It's platform specific...
    See the FAQ: http://faq.cprogramming.com/cgi-bin/...&id=1043284392

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes and no.

    First of all, any "colour" output would be quite non-standard. So it would only work on certain systems. This by itself means that it's not part of printf itself.

    You can make printf (or similar functions) output escape codes to print in colour, move the cursor around, etc, etc.

    http://en.wikipedia.org/wiki/ANSI_escape_code

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50
    hi
    hey guys I found this code also.But It works only with turbo C .

    Code:
    #include <conio.h>
    
    int main(void)
    {
    int BLACK=0;
    int RED = 12;
    
    textcolor(RED);
    textbackground(BLACK);
    cprintf("It worked!");
    getch();
    return 0;
    }

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Anuradh_a View Post
    hi
    This is my first post.everybody know that printf() prints the screen. is their any way
    that I can give color full out put using printf(). I mean is it possible to output colored
    text to the dos prompt.
    thanks.
    ncurses, although to use in on Windows you need Cygwin.

  6. #6
    Just a Human Anuradh_a's Avatar
    Join Date
    Jan 2008
    Posts
    50
    hi
    Cygwin Is it a linux IDE .I have downloaded a setup file which install from network dirctly. Can you give me direct link download it as a full package.
    thanks.

  7. #7
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe what you want is SetConsoleTextAttribute(). It has a lot of parameters you pass it, to set up the colors. This is a tiny snippet of code from Visual C. All it did was print a number of spaces in the color and intensity that I needed for that part of the display for a console (text), game program.

    Code:
    	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 
    	BACKGROUND_GREEN | BACKGROUND_RED |	BACKGROUND_BLUE | BACKGROUND_INTENSITY |
    	FOREGROUND_GREEN | FOREGROUND_RED);
    	printf("                                          \n ");
    If you google this, SetConsoleTextAttribute, you'll get what you want. Much easier to work with than ANSI escape codes.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM