Skip to content
Related Articles

Related Articles

Colorizing text and console background in C++
  • 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Output:

Attention reader! Don’t stop learning now. Get hold of all the important C++ Foundation and STL concepts with the C++ Foundation and STL courses at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :