Search This Blog

Friday 31 January 2014

Changing the TextColor of Console in Code::Blocks

Hello Programmers,
In this post you will learn about changing the Color of Text on Console using Code::Blocks IDE and MinGW Compiler.
Here we are using a function sytem() which is defined in stdlib.h header file.
The function system() can be used in following two ways:
  • Passing a color code just to change the foreground of the text.
    Syntax:           system("COLOR <color code>");
    Example:        system("COLOR 4"); 
  • Passing two color codes, one for background and one for foreground of the console.
    Syntax:           system("COLOR <color codeF><color codeB>");
                           color codeF is for foreground and
                           color codeB is for background.
    Example:        system("COLOR AC"); 



  1. /*
  2. Aditya's Console Color Changing Code Example
  3. Compiled On:    MigGW (CODE::BLOCKS)
  4. This code will change the Foreground color of the console text.
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. int main()
  9. {
  10.     system("COLOR 6");
  11.     printf("Welcome to the COLOUR changing Console Application!\n");
  12.     return 0;
  13. }

Using system("COLOR 6");


       /*Code to change Foreground and Background of console.*/
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. int main()
  4. {

  5. /* 
    Along with COLOR you can pass two codes 

    Here I have used F for Foreground and C for Background


    */ 
  6.     system("COLOR FC");
  7.     printf("Welcome to the COLOUR changing Console Application! \nThis code changes ForeGround and BackGround");
  8.     return 0;
  9. }


    Using system("COLOR FC")
Once you have understood the concept of using system("COLOR <color code>"); try to play with the given color codes.


/*
Color Codes:
0 = Black
1 = Blue
2 = Green
3 = Aqua
4 = Red
5 = Purple
6 = Yellow
7 = White
8 = Gray
9 = Light Blue
A = Light Green
B = Light Aqua
C = Light Red
D = Light Purple
E = Light Yellow
F = Bright White
*/