Skip to content
Related Articles

Related Articles

Colorizing text and console background in C++

Improve Article
Save Article
Like Article
  • Last Updated : 27 Jan, 2021

In C++ programming, the default background of the output screen is black and the text color is the white color, the task is to color both the background and text color in the output screen.

Header File:

The header file required to color the text and background can be either of the given header files:

#include <windows.h>
or
#include <stdlib.h>

Syntax of the color console:

HANDLE console_color;

// Color of the console
console_color = GetStdHandle(STD_OUTPUT_HANDLE);

// P is color code according to your need.
SetConsoleTextAttribute(console_color, P);

Below is the program for the same:

C++




// C++ program for the coloring the
// background and text with
// different color
#include <iostream>
  
// Header file to change color of
// text and background
#include <windows.h>
using namespace std;
  
// Driver Code
int main()
{
    // Color of the console
    HANDLE console_color;
    console_color = GetStdHandle(
        STD_OUTPUT_HANDLE);
  
    // Print different colors from 1
    // to 50 on the output screen
    for (int P = 1; P < 50; P++) {
  
        // P is color code of the
        // corresponding color
        SetConsoleTextAttribute(
            console_color, P);
  
        // Print Statement
        cout << P << " Hello Geeks, "
             << "good night!!!";
    }
  
    return 0;
}

Output:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!