Hi, everyone, it’s been a while.
Today, I’m posting my last post of this month. It’s nothing about configuration, but it might help you when debugging C codes. I’m talking about coloring standard output texts. You know, as we go through debugging process, sometimes is useful to print out values of a variable just to make sure that such variable is holding what we want, but when there are many printed lines we may lose readability.
Hopefully, we can color our printed lines and there is a simple way to do it in C codes. I think it would be helpful to see what we want if the print is colored. So, for that, we only need to use something like this “\x1b[31m” when calling, for instance, printf(). I know it is hard to remember such sequences of characters… It’s been a long time since when I used it for the last time. Long time ago.
Recently, I needed it. However, I wasn’t remembering such special sequence of characters and, in order to remember, I went to google search :D. And, whithout looking for, I found a cool way here to color my prints.
I’m putting some examples below.
#define ANSI_COLOR_RED "\x1b[31m" #define ANSI_COLOR_GREEN "\x1b[32m" #define ANSI_COLOR_YELLOW "\x1b[33m" #define ANSI_COLOR_BLUE "\x1b[34m" #define ANSI_COLOR_MAGENTA "\x1b[35m" #define ANSI_COLOR_CYAN "\x1b[36m" #define ANSI_COLOR_RESET "\x1b[0m"
After defined, we are free to use it.
printf(ANSI_COLOR_CYAN "Colored line" ANSI_COLOR_RESET); printf(ANSI_COLOR_CYAN "Colored line with integer argument %d" ANSI_COLOR_RESET, 5); printf(ANSI_COLOR_CYAN "Colored line with integer argument %d and new line" ANSI_COLOR_RESET "\n", 10);