DEV Community

Cover image for Problems with the compiler to change the color to something in C, this is the solution
Joan Ariel Peralta Sosa
Joan Ariel Peralta Sosa

Posted on

3 3

Problems with the compiler to change the color to something in C, this is the solution

I always ran into the problem of my compiler I could not apply colors to something in C, after researching and searching many places I finally found the solution, we simply apply this line of code to the part that we want to change the color and then we have to repeat it with the original color, since the color is changed to everything below this line. It is perfect for the MINGW compiler, the cl function is what calls the color and the number is the selected color, these colors are the same ones used to change the color in win console.

this is the code
void cl(int color)
{
HANDLE h=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(h,color);
}

int main()
{
cl(5);
printf("HI");
cl(2);
printf("\nMM");
getche();
}

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more