Find the answer to your Linux question:
Results 1 to 7 of 7

Thread: Color Console

Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Color Console

    Using C/C++, how do you output text in different colors?

    In Windows you can use
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HA NDLE), MyColor);
    but this doesn't work in *nix

    Thanks in advance

  2. #2
    Just Joined!
    Join Date
    Nov 2002

    Text Colors

    Text color output is not defined in ANSI C/C++. Instead the creators of the language left that to be operating system dependent. In Linux, to change text color you must issue what are known as terminal commands. To do this you just change your output statement to contain a terminal command.

    For example, see the below program.

    #include <stdio.h>

    int main()
    printf("Hello, world!\n");

    To print "Hello, world.\!" in red I would change the printf statement to read:
    printf("\033[22;31mHello, world!");

    \033[22;31m is the terminal command: it will not be output to the terminal, instead it issues a command that changes all proceeding output to be printed in red

    I just figured that out today, so don't look to me as an expert. Through some hacking I discovered that there are 16 available terminal colors. I don't believe there are any more, and the limitation of 16 would make sense. To try different colors, just change the numbers after the left bracket.

    \033[22;30m is black
    \033[22;31m is red
    \033[22;32m is green

    I would just define macros for each color in your 'c' program if I were you.
    I have never changed the output text color in a C/C++ program until today. I use them in shell scripts though to change the colors of my prompt! I just took what I've done in my ksh script and applied that to C/C++. If you want a definitive reference on the colors and other available terminal commands I would do a search for the Terminal HowTO on google or on your linux box if the docs were installed.

    Also, I have done very little with this, but there is a library for formatting window-like screens on the terminal. This library is called curses. Terrible name, I know, but you may find this to be of value as well. Can't help you there either as I've only fooled around with curses in a few short test programs. I've never used it for any real project.

    All the colors that I have found are:
    \033[22;30m - black
    \033[22;31m - red
    \033[22;32m - green
    \033[22;33m - brown
    \033[22;34m - blue
    \033[22;35m - magenta
    \033[22;36m - cyan
    \033[22;37m - gray
    \033[01;30m - dark gray
    \033[01;31m - light red
    \033[01;32m - light green
    \033[01;33m - yellow
    \033[01;34m - light blue
    \033[01;35m - light magenta
    \033[01;36m - light cyan
    \033[01;37m - white

    Remember, I hacked this out a while back, so check the Terminal HowTo for a better reference.

    Don't forget that you can use these to change the color of your shell's command prompt as well. That's real fun. You should see some of the interesting color combinations I've got for shell prompts!

    Happy hacking!

  3. #3
    Just Joined!
    Join Date
    Nov 2002

    More terminal commands

    Go to:
    and download the VT100 User Guide.

  4. $spacer_open
  5. #4
    Linux Guru
    Join Date
    Oct 2001
    Täby, Sweden
    Now that's a really bad idea. Using the console escape sequences directly might render your program utterly unportable. Use ncurses instead. There are the man pages to learn about this, or you could probably find a tutorial and/or docs on the internet (try google). There are also the XCURSES section of the single unix specification (see
    If you don't want the program to be portable, you can of course use the escape sequences. More info on these can be found in console_codes(4) on a linux system. There you'll also find the linux extensions that aren't part of the vt100 specs.
    I'd still recommend ncurses, though. It does a lot of things automagically for you.

  6. #5
    Just Joined!
    Join Date
    Nov 2002
    Dolda2000: That's true. Thanks for pointing that out. If you want portability use ncurses. I didn't know about console_codes(4). Thanks for the tip! I imagine that the ncurses library has to use terminal escape codes as well, but done in a more portable fashion. Whether or not it's a bad idea depends on what you are doing. For my purposes in shell scripts, terminal escape sequences was the simplest way to do it.

  7. #6
    Thanks, slant6, that's exactly what I was looking for.

  8. #7

    The best!

    Really really thanks a lot for this code! This really helped me and i was able to save lines in my programs because of this..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts