How to change the text color of console using C
destination source:https://www.programming-techniques.com/2011/08/how-to-change-text-color-of-console.html
The following code changes the text color of console to the color defined by color value supplied. You must include <windows.h> to use this function. It works on windows only.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <stdio.h> #include <windows.h> #include <conio.h> void SetColor(int ForgC) { WORD wColor; HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE); CONSOLE_SCREEN_BUFFER_INFO csbi; if(GetConsoleScreenBufferInfo(hStdOut, &csbi)) { wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F); SetConsoleTextAttribute(hStdOut, wColor); } return; } int main(){ int fore; printf("Enter text color value: "); scanf("%d",&fore); SetColor(fore); printf("Your color will be like this"); getch(); return 0; } |
Output

Related Article
destination source:https://www.programming-techniques.com/2011/08/how-to-change-text-color-of-console.html