DEV Community

Cover image for Enhancing Your C++ Terminal Output with ANSI Escape Codes
Ahmed Muneeb
Ahmed Muneeb

Posted on

Enhancing Your C++ Terminal Output with ANSI Escape Codes

In this article, we’ll explore how to use ANSI escape codes to add color and style to your C++ console applications. ANSI escape codes provide an easy way to control text formatting, including text color, background color, boldness, and underlining, directly in the terminal. This guide will show you how to implement these features to make your C++ programs more visually appealing.

What Are ANSI Escape Codes?

Definition:
ANSI escape codes are sequences of characters that control the appearance of text in the terminal. These codes are used to style text with attributes such as color, background, and text effects.

Key Points:

  • Part of the ANSI standard for terminal control.
  • Widely supported across Unix-based systems (Linux/macOS) and modern terminal emulators.
  • Can be used to make terminal applications more interactive and visually engaging.

Basic Syntax of ANSI Escape Codes

The syntax for an ANSI escape code is as follows:

\033[<code>m
Enter fullscreen mode Exit fullscreen mode
  • \033: Escape character (ASCII code 27).
  • [: CSI (Control Sequence Introducer).
  • <code>: The style or color code (e.g., 31 for red).
  • m: End of the code.

Example Reset Code:

  • \033[0m: Resets the terminal attributes to default (removes all styles).

Common ANSI Codes

Here are some of the most commonly used codes:

  • Text Styles:

    • 0 → Reset
    • 1 → Bold
    • 4 → Underline
    • 5 → Blink (may not work on all terminals)
    • 7 → Inverse (reversed colors)
  • Text Colors (Foreground):

    • 30 → Black
    • 31 → Red
    • 32 → Green
    • 33 → Yellow
    • 34 → Blue
    • 35 → Magenta
    • 36 → Cyan
    • 37 → White
  • Background Colors:

    • 40 → Black
    • 41 → Red
    • 42 → Green
    • 43 → Yellow
    • 44 → Blue
    • 45 → Magenta
    • 46 → Cyan
    • 47 → White

Practical Examples of ANSI Escape Codes in C++

Example 1: Changing Text Colors

#include <iostream>

int main() {
    std::cout << "\033[31mThis is red text\033[0m" << std::endl;
    std::cout << "\033[32mThis is green text\033[0m" << std::endl;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Changing Text Colors
Example 2: Combining Text Styles and Colors

#include <iostream>

int main() {
    std::cout << "\033[1;34mBold Blue Text\033[0m" << std::endl;
    std::cout << "\033[4;33mUnderlined Yellow Text\033[0m" << std::endl;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Combining Text Styles and Colors
Example 3: Background Colors

#include <iostream>

int main() {
    std::cout << "\033[41;37mRed Background Text\033[0m" << std::endl;
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Background Colors

Advanced ANSI Escape Codes

You can also use more advanced escape codes like blinking text or reverse colors:
Blinking Text:

std::cout << "\033[5mThis text blinks\033[0m" << std::endl;
Enter fullscreen mode Exit fullscreen mode

Reverse Colors (Inverted):

std::cout << "\033[7mThis text has inverted colors\033[0m" << std::endl;
Enter fullscreen mode Exit fullscreen mode

Limitations and Compatibility

  • Windows Users: On older versions of Windows, ANSI codes might not work in the default command prompt. However, newer versions of Windows Terminal or WSL support them.
  • Terminal Behavior: Some terminals may not support certain codes, like blinking text, or may have it disabled by default.
  • Online Compilers: Most online compilers won't show colors since they simulate a terminal environment that doesn’t interpret escape codes.

Conclusion

By integrating ANSI escape codes into your C++ projects, you can create more interactive and visually appealing console applications. Whether you're adding simple color to the text or experimenting with advanced styles, ANSI codes can enhance the user experience in terminal-based programs.

Top comments (3)

Collapse
 
pgradot profile image
Pierre Gradot

You should also mentioned that they are many libraries available on GitHub for this. Example github.com/ikalnytskyi/termcolor We really don't want to do this by hand ;)

Collapse
 
ahmedmuneeb profile image
Ahmed Muneeb

The problem is that you have to install the libraries on every device. But most of the terminal have a support for ANSI Escape Codes so thats why its better.

Collapse
 
pgradot profile image
Pierre Gradot

You don't necessarily have to install libraries on every device. The library I have linked in a header-only library, you just include it and compile your code, that's it. Once your executable is produced, there is no external sign of the library.