How can I output colored text using "printf" on both Mac OS X and Linux?
Use SetConsoleTextAttribute() Method to Change Console Color in C++ SetConsoleTextAttribute is the Windows API method to set output text colors using different parameters. This function sets the attributes of characters written to the console screen buffer by the WriteFile or WriteConsole functions.
You can use the ANSI colour codes. Here's an example program:
#include <stdio.h> int main(int argc, char *argv[]) { printf("%c[1;31mHello, world!\n", 27); // red printf("%c[1;32mHello, world!\n", 27); // green printf("%c[1;33mHello, world!\n", 27); // yellow printf("%c[1;34mHello, world!\n", 27); // blue return 0; }
The 27
is the escape
character. You can use \e
if you prefer.
There are lists of all the codes all over the web. Here is one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With