'There was a function in C to adjust background color? (It was actually a Dos Command)

I am looking for the system function to adjust background color. It was like

system("color",somecolorcodes);

Does anyone know about it?

On Windows Xp or 7!



Solution 1:[1]

system("cls");  //clears the screen
system("color F0"); //Creates Bright White Background with black text
system("type struct3.c  struct2.c");  /*prints the file struct3 and struct2 in the 
                                                                             console*/

system() is a really useful function included in Windows.h library. Apparently we can do many other tasks with this function so I was searching for it when I came across this thread.

Edit:While looking at the commands in command prompt I realized that the above examples are commands in the command prompt and tried using other commands like time, help, del etc in the system() function and figured all the commands that we use in command prompt can be used by System() function. For that we write the commands in the System() function like below system("command"); Even though C is case sensitive the command inside system() is not case sensitive like in command prompt.

Solution 2:[2]

You can use the SetConsoleTextAttribute function in Windows. This will let you output text in different colors at the same time, while calling "color" doesn't.

There are also others that are less coarse -- search for color in this listing of console functions.

Also, if you're looking for a cross-platform approach take a look, for instance, at this file from Musepack.

Solution 3:[3]

A somewhat more portable way of doing this (not DOS or Windows specific) is:

printf("\033[%dm", 40 + color);   /* set background color */

The corresponding way to set the foreground color is:

printf("\033[%dm", 30 + color);   /* set foreground color */

These work with colors:

0    black
1    red
2    green
3    yellow/brown
4    blue
5    magenta
6    cyan
7    white

These aren't truly portable, either; they work wherever "ANSI control sequences" are implemented in your terminal, terminal emulator, or console.

Under Linux and/or xterm, prefixes 90 (for foreground) and 100 (for background) seem to work, also, perhaps with a slightly different set of colors.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Artefacto
Solution 3 Steve Summit