How to Change Console Color in C++
- Understanding Console Colors
- Changing Console Color on Windows
- Changing Console Color on UNIX-like Systems
- Conclusion
- FAQ

Changing the console color in C++ can enhance the user experience by adding visual appeal and clarity to the output. Whether you’re developing a simple text-based game or a command-line utility, customizing the console’s appearance can make your application stand out.
In this article, we will explore various methods to change console colors in C++. We will provide practical examples and detailed explanations to help you understand how to implement these techniques effectively. By the end of this article, you will have a solid grasp of how to manipulate console colors using C++.
Understanding Console Colors
Before diving into the code, it’s essential to understand how console colors work. Most console applications support a limited palette of colors, typically defined by the operating system. The colors can usually be modified by sending specific commands or using libraries designed for console manipulation.
In C++, the most common way to change console colors is by utilizing the Windows API on Windows systems or ANSI escape codes on UNIX-like systems. Each method has its own syntax and requirements, so let’s explore both approaches in detail.
ANSI escape codes are a relatively portable way to solve this problem. Escape codes are byte sequences starting mostly with an ASCII Escape character and bracket character followed by parameters. These characters are embedded into the output text, and the console interprets the sequences as commands rather than text to display. ANSI codes include multiple color formats, full details of which can be seen on this page. In the following code example, we demonstrate defining several color characters as macros and later include these symbols in the printf
string arguments. Notice that printf
concatenates multiple double-quoted strings passed in the first parameter place.
Changing Console Color on Windows
To change the console color in a Windows environment, you can use the Windows API. This method involves including the windows.h
header file and using the SetConsoleTextAttribute
function. Here’s a simple example to demonstrate how to change the text color:
#include <iostream>
#include <windows.h>
void setColor(int color) {
HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hConsole, color);
}
int main() {
setColor(10); // Set text color to green
std::cout << "This text is green!" << std::endl;
setColor(12); // Set text color to red
std::cout << "This text is red!" << std::endl;
setColor(7); // Reset to default
std::cout << "This text is default color." << std::endl;
return 0;
}
Output:
This text is green!
This text is red!
This text is default color.
In this example, we defined a setColor
function that takes an integer representing the desired color. The GetStdHandle
function retrieves the standard output handle, and SetConsoleTextAttribute
changes the text color based on the provided integer. The integers correspond to different colors, such as 10 for green and 12 for red. After changing the color, we print out messages to demonstrate the effect.
Changing Console Color on UNIX-like Systems
If you’re working in a UNIX-like environment, such as Linux or macOS, you can use ANSI escape codes to change console colors. This method is straightforward and works well across various terminal emulators. Here’s an example of how to use ANSI codes to change text color:
#include <iostream>
int main() {
std::cout << "\033[32m"; // Set text color to green
std::cout << "This text is green!" << std::endl;
std::cout << "\033[31m"; // Set text color to red
std::cout << "This text is red!" << std::endl;
std::cout << "\033[0m"; // Reset to default
std::cout << "This text is default color." << std::endl;
return 0;
}
Output:
This text is green!
This text is red!
This text is default color.
In this code snippet, we use escape sequences starting with \033
to change the text color. The 32m
code sets the text color to green, while 31m
sets it to red. Finally, 0m
resets the color back to the terminal’s default. This method is widely supported and allows for more flexibility in styling your console output.
Conclusion
Changing console colors in C++ is a simple yet effective way to enhance the user interface of your applications. Whether you are using the Windows API or ANSI escape codes, customizing the console can make your output more engaging and visually appealing. By following the examples provided in this article, you can easily implement color changes in your own C++ projects. Experiment with different colors and styles to create a unique console experience that stands out.
FAQ
-
How do I change the console color in C++ on Windows?
You can use the Windows API, specifically theSetConsoleTextAttribute
function, to change console colors. -
Can I use ANSI codes to change console color on Windows?
Yes, ANSI codes can work on Windows 10 and later versions if virtual terminal processing is enabled. -
What color codes can I use in C++?
Color codes vary by system. For Windows, you can use integers ranging from 0 to 15. For ANSI codes, you can use codes like 30-37 for foreground colors and 40-47 for background colors. -
Is it possible to change background color as well?
Yes, both methods allow you to change the background color. In Windows, you can combine foreground and background color codes, while in ANSI codes, you can use background-specific codes. -
Are there libraries available for easier color management in C++?
Yes, libraries like ncurses (for UNIX-like systems) or color libraries for Windows can simplify color management in console applications.
Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.
LinkedIn Facebook