C color text in terminal applications in windows

Since you want a C and Windows specific solution, I’d recommend using the SetConsoleTextAttribute() function in the Win32 API. You’ll need to grab a handle to the console, and then pass it with the appropriate attributes.

As a simple example:

/* Change console text color, then restore it back to normal. */
#include <stdio.h>
#include <windows.h>

int main() {
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
    WORD saved_attributes;

    /* Save current attributes */
    GetConsoleScreenBufferInfo(hConsole, &consoleInfo);
    saved_attributes = consoleInfo.wAttributes;

    SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE);
    printf("This is some nice COLORFUL text, isn't it?");

    /* Restore original attributes */
    SetConsoleTextAttribute(hConsole, saved_attributes);
    printf("Back to normal");

    return 0;

For more info on the available attributes, look here.

Hope this helps! 🙂

Leave a Comment