• Uncategorized

How to change the text color of console using C

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.

#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

 

text_color

SHARE How to change the text color of console using C

You may also like...


Warning: Undefined array key 0 in /www/wwwroot/followtutorials.com/wp-content/themes/hueman/single.php on line 9

Warning: Attempt to read property "cat_name" on null in /www/wwwroot/followtutorials.com/wp-content/themes/hueman/single.php on line 9

Warning: Undefined array key 0 in /www/wwwroot/followtutorials.com/wp-content/themes/hueman/single.php on line 14

Warning: Attempt to read property "category_parent" on null in /www/wwwroot/followtutorials.com/wp-content/themes/hueman/functions.php on line 37

Warning: Undefined array key 0 in /www/wwwroot/followtutorials.com/wp-content/themes/hueman/single.php on line 15

Warning: Attempt to read property "category_parent" on null in /www/wwwroot/followtutorials.com/wp-content/themes/hueman/single.php on line 15
Share