Parameters
| Name | Type | Description |
|---|---|---|
color | int | The index or constant name representing the background color. Valid choices are the integers 0 through getmaxcolor(). |
The color index maps directly to the standard 16 BGI colors (constants are defined as capital letters in graphics.h). On typical VGA screens, these constants range from BLACK (0) to WHITE (15). Using named constants like BLUE, DARKGRAY, or LIGHTGREEN is highly recommended over raw integer values to ensure readability.
Return Value
setbkcolor() returns void. To verify the current background color index setting at any point in your code, call the complementary function getbkcolor(), which returns the current active integer color index.
Code Examples
Basic background change
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
initgraph(&gd, &gm, "");
// Set background to dark blue
setbkcolor(BLUE);
// Set drawing color to yellow and draw a circle
setcolor(YELLOW);
circle(320, 240, 100);
getch();
closegraph();
return 0;
}
This program sets the background color of the screen to solid blue and draws a centered yellow circle. The result is a crisp yellow circle on a blue background, illustrating basic color contrast configurations.
Interactive background cycler
#include <graphics.h>
#include <conio.h>
int main() {
int gd = DETECT, gm;
int col = 0;
initgraph(&gd, &gm, "");
setcolor(WHITE);
outtextxy(50, 50, "Press any key to cycle background colors...");
while(!kbhit()) {
// Change background color index
setbkcolor(col);
col = (col + 1) % 16;
delay(500); // Wait 500 milliseconds
}
getch(); // Clear key from buffer
closegraph();
return 0;
}
This example demonstrates BGI's palette behavior. Every half-second, the background color index cycles from 0 to 15. Notice that the text remains visible and the background color updates dynamically under it without erasing the text itself.
Common Mistakes
Forgetting drawing color contrast
If you call setbkcolor(BLUE) and then call setcolor(BLUE), any subsequent lines or circles will be drawn in blue on a blue background, rendering them completely invisible. Always ensure high contrast between your drawing color (foreground) and background color (e.g., yellow lines on a blue background, or white lines on a dark gray background).
Assuming setbkcolor() clears the screen
In some compiler/emulation setups, changing the background color register might only affect newly rendered pixels if immediate palette-swapping is not supported. To write robust, portable code that works across Turbo C++, DOSBox, and Web emulators, always call cleardevice() right after setbkcolor(). This forces the entire buffer to refresh with the new background color.
Using RGB values instead of palette indices
Like setcolor(), setbkcolor() takes an index between 0 and 15 (in VGA mode). Passing a hex color code (like 0x0000FF for blue) will not work and will likely result in the background reverting to black or light gray due to integer truncation.
Try it online
Run these snippets online. Copy-paste them into the interactive compiler editor panel to run C graphics programs directly in the browser.