Click here to Skip to main content
15,038,237 members
Articles / Programming Languages / C++
Posted 17 Nov 2006


91 bookmarked

Add color to your std::cout

Rate me:
Please Sign up or sign in to vote.
4.82/5 (43 votes)
17 Nov 2006
A tiny library that adds color to CRT programs.


This article illustrates a snippet that permits to add color to console messages.


Well, does it really need any comments?

Using the code

This sample shows how to use the library:

#include "ConsoleColor.h"

std::cout << green << "This text is written in green" 
          << white << std::endl;
          << "This text has a really exiting color !" 
          << white << std::endl;

Now here is the "library":

// ConsoleColor.h

#pragma once
#include <iostream>
#include <windows.h>

inline std::ostream& blue(std::ostream &s)
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
    SetConsoleTextAttribute(hStdout, FOREGROUND_BLUE
    return s;

inline std::ostream& red(std::ostream &s)
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
    return s;

inline std::ostream& green(std::ostream &s)
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
    return s;

inline std::ostream& yellow(std::ostream &s)
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
    return s;

inline std::ostream& white(std::ostream &s)
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE); 
    return s;

struct color {
    color(WORD attribute):m_color(attribute){};
    WORD m_color;

template <class _Elem, class _Traits>
      operator<<(std::basic_ostream<_Elem,_Traits>& i, color& c)
    HANDLE hStdout=GetStdHandle(STD_OUTPUT_HANDLE); 
    return i;

// Copyleft Vincent Godin


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Vincent Godin
Web Developer
France France
No Biography provided

Comments and Discussions

GeneralAlmost perfect Pin
Member 139698021-Sep-18 17:21
MemberMember 139698021-Sep-18 17:21 
NewsI wrote a similar, but more... Pin
chkesp8-Nov-14 20:35
Memberchkesp8-Nov-14 20:35 
GeneralMy vote of 4 Pin
Vijay_vijay14-Aug-14 20:40
MemberVijay_vijay14-Aug-14 20:40 
GeneralMy vote of 3 Pin
Throwaway1113-Jun-14 2:43
MemberThrowaway1113-Jun-14 2:43 
GeneralMy vote of 1 Pin
Throwaway1113-Jun-14 2:42
MemberThrowaway1113-Jun-14 2:42 
GeneralIt is not x-platform Pin
Throwaway1113-Jun-14 2:38
MemberThrowaway1113-Jun-14 2:38 
GeneralMy vote of 4 Pin
john_th26-Mar-13 20:21
Memberjohn_th26-Mar-13 20:21 
GeneralMy vote of 5 Pin
Ahmed Charfeddine13-Aug-12 15:14
MemberAhmed Charfeddine13-Aug-12 15:14 
GeneralYou shouldn't refere the r-value Pin
Evil.Ghost13-Jan-09 17:22
MemberEvil.Ghost13-Jan-09 17:22 
GeneralRe: You shouldn't refere the r-value Pin
daihouxiang13-May-10 1:38
Memberdaihouxiang13-May-10 1:38 
GeneralNot perfect, compiles sometimes Pin
BrianFx26-Nov-08 4:01
MemberBrianFx26-Nov-08 4:01 
GeneralRe: Not perfect, compiles sometimes Pin
Evil.Ghost13-Jan-09 17:17
MemberEvil.Ghost13-Jan-09 17:17 
SuggestionRe: Not perfect, compiles sometimes Pin
ABharadwaj28-Nov-12 23:51
MemberABharadwaj28-Nov-12 23:51 
GeneralPerfect Pin
b3b9t31-Oct-08 20:23
Memberb3b9t31-Oct-08 20:23 
QuestionWhat did the parameter has been transfer? Pin
Karlzheng22-Mar-07 16:03
MemberKarlzheng22-Mar-07 16:03 
Generalperfect Pin
cooleaf19-Dec-06 15:29
Membercooleaf19-Dec-06 15:29 
GeneralRe: perfect Pin
zhangpingfly21-Dec-07 20:46
Memberzhangpingfly21-Dec-07 20:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.