Howto Print A Colorful String on Linux Terminal With C Program
As we know, we can display a colorful string on linux terminal with bash shell,
but what about c program?
In this post, I’ll talk about howto print a colorful string on linux terminal with c program.
At first, I’ll give you an introduction about foregroud and background colors, which are as following:
Foreground Colors Background Colors
-----------------------------------------
Value Color Value Color
-----------------------------------------
30 Black 40 Black
31 Red 41 Red
32 Green 42 Green
33 Yellow 43 Yellow
34 Blue 44 Blue
35 Purple 45 Purple
36 Cyan 46 Cyan
37 White 47 White
I want to display a colorful string on the terminal, and the bash shell command as follows:
1 | $ echo -e “\033[41;33mHello, this is a colorful characters string example.\033[0m” |
Now, I do it with c program colors.c, which content is:
1 |
|
Save it as colors.c and compile with gcc as following:
1 | $ gcc colors.c -o colors |
Then run it, and what happens?
1 | $ ./colors |
Yes, as you know, there is a colorful string printed on the terminal.