There is no traditional way to show colored output in your console in gcc compiler like turbo C++. Turbo C++ has built in header file (conio.h) to perform this task. But in gcc this can be achieved through escape sequences. e.g. look at this program output
using gcc compiler - Online C Compiler - online editor
#include <stdio.h> /* printf */
int main(void)
{
printf("\033[0mNormal text\033[0m\n");
printf("\033[31mRed text\033[0m\n");
printf("\033[4mUnderlined text\033[0m\n");
printf("\033[37;104mGray on light blue\033[0m\n");
printf("\033[1;37;44mBright white on blue\033[0m\n");
printf("\033[1;37;41mBright white on light red\033[0m\n");
printf("\033[5;93mBlinking yellow\033[0m\n");
return 0;
}
using g++ compiler: Online C++ Compiler - online editor
#include <iostream> /* cout, endl */
int main()
{
using std::cout;
using std::endl;
cout << "\033[0mNormal text\033[0m" << endl;
cout << "\033[31mRed text\033[0m" << endl;
cout << "\033[4mUnderlined text\033[0m" << endl;
cout << "\033[37;104mGray on light blue\033[0m" << endl;
cout << "\033[1;37;44mBright white on blue\033[0m" << endl;
cout << "\033[1;37;41mBright white on light red\033[0m" << endl;
cout << "\033[5;93mBlinking yellow\033[0m" << endl;
return 0;
}
Color codes and Escape Sequences:
n escape sequence includes 3 integer values: an attribute, a foreground color, and a background color. If you don't provide one of the three values, the default will be used.Attributes | Foreground color | Background color |
---|---|---|
00 = normal 01 = bold 04 = underlined 05 = blinking 07 = reversed 08 = concealed | 31 = red 32 = green 33 = orange 34 = blue 35 = purple 36 = cyan 37 = grey 90 = dark grey 91 = light red 92 = light green 93 = yellow 94 = light blue 95 = light purple 96 = turquoise | 40 = black 41 = red 42 = green 43 = orange 44 = blue 45 = purple 46 = cyan 47 = grey 100 = dark grey 101 = light red 102 = light green 103 = yellow 104 = light blue 105 = light purple 106 = turquoise |
No comments:
Post a Comment