How to color of text in C-Language using GCC Compiler
In C-Language when you will use text color then you must have to know about the color code of C language.
Name | Value
|
Black | 0
Blue | 1
Green | 2
Cyan | 3
Red | 4
Magenta | 5
Brown | 6
Light Gray | 7
Dark Gray | 8
Light Blue | 9
Light Green | 10
Light Cyan | 11
Light Red | 12
Light Magenta| 13
Yellow | 14
White | 15
-----------------------
OR
-----------------------
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
Example 1
#include
int main()
{
/* Some list of combinations
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 */
printf("This is a console color change program\n");
system("COLOR F2"); /* This will change the bgcolor F - White and textcolor to 2- Green */
getchar();
return 0;
}
Example 2
#include
#include
int main()
{
system("COLOR FC");
printf("Welcome to the color changing application!\n");
printf("Press any key to change the background color!\n");
getch();
system("COLOR 6C");
printf("Now the background color is Yellow and Text Color is light Red\n");
printf("Press any key to exit...");
getch();
return 0;
}
Example 3
#include //This is the header file for windows.
#include //C standard library header file
void SetColor(int ForgC){
WORD wColor;
//This handle is needed to get the current background attribute
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
CONSOLE_SCREEN_BUFFER_INFO csbi;
//csbi is used for wAttributes word
if(GetConsoleScreenBufferInfo(hStdOut, &csbi)){
//To mask out all but the background attribute, and to add the color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
SetConsoleTextAttribute(hStdOut, wColor);
}
}
int main()
{
printf("Test color"); //Here the text color is white
SetColor(4); //Function call to change the text color
printf("Test color"); //Now the text color is green
return 0;
}
agyanadda
Author & Editor
Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.
0 $type={blogger}:
Post a Comment