Here in this post, we will learn how we can change the background color and font color in c/c++ language in the cmd or command prompt or console output screen with some basic concept regarding color command.

Change background color in of console output in c/c++ language

Change background color in c language


    The System() Function in C/C++

    The system() function is a part of the C/C++ standard library. It is used to pass the commands that can be executed in the command processor or the terminal of the operating system and finally returns the command after it has been completed.

    <stdlib.h>  or  <cstdlib> or <stdio.h> should be included to call this function.

    system(const char *command);

    Basically, the system refers to the command prompt(cmd), in which we get many color to chose by entering some commands.

    Some Concept regarding color command

    You can use command "color  /?" for help to get more information about the color command. 

    As you can see in the picture below:

    Change background color in c language

    To get the background color you want, enter the color then the option for the background color followed by the font color. 

    color backgroundcolorfontcolor

    For example:
    color f2

    Here f signify the Bright white background color and 2 signify the green font color.

    Various Color Codes

    Some list of color combinations for using different color code in system() function

    Here is how you can enter the color code in the system function
    system("COLOR enter here the color code"); 
     0              Black               
     2 Green
     3 Aqua
     4 Red
     5 Purple
     6 Yellow
     8 Gray
     9 Light Blue
     A Light Green
     B Light Aqua
     C Light Red
     D Light Purple
     E Light Yellow
     F Bright White

    Background color change program in c

    Now you have the basic knowledge about the Command prompt color command. Just use the same concept in the C program below.

    #include <stdio.h>
    int main()
        printf("This is a console color change program\n");
        system("COLOR F2"); // Background color changing function
        return 0;

    Output screen


    Here you can see that the background color has been changed to bright white and font color to green color.    

    Post a Comment

    Previous Post Next Post