In this tutorial, I will guide you through “How to Change Text Color in CodeBlocks Console Window”. The source code here is what you’re looking for if you want to change the text color in the console window of Code::Blocks without using graphics. You can simply copy the code, and compile it in Code::Blocks IDE. The source code is written in C language, and I have included comments in the code to help you understand it better.
The source code consists of two header files, windows.h and stdio.h which are the header file for windows and C standard library respectively. The function, void SetColor(int ForgC) takes integer values, and is responsible for the change in text color in console window.
Currently the text color displayed is Green color. You can change your desired text color by passing integer values (in the range of 0 to 256) in SetColor( ) function.
//How to Change Text Color in CodeBlocks Console Window
#include <windows.h> //This is the header file for windows.
#include <stdio.h> //C standard library header file
void SetColor(int ForgC);
printf("Test color"); //Here the text color is white
SetColor(34); //Function call to change the text color
printf("Test color"); //Now the text color is green
void SetColor(int ForgC)
//This handle is needed to get the current background attribute
HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
//csbi is used for wAttributes word
//To mask out all but the background attribute, and to add the color
wColor = (csbi.wAttributes & 0xF0) + (ForgC & 0x0F);
Here’s an output screen of what happened in the console window. As you can see, previously the text color was white, but after the function call, the text color changed to green.
With this post, I hope you learnt how to change text color in CodeBlocks console window. You can also learn how to highlight or change text background color and how to change console window background color, all in Code::Blocks. If you have any queries, bring them up to me from the comments section.