Stdlib and color output in C

I am making a simple application that requires color output. How can I make my output color like emacs and bash do?

I do not care about Windows because my application is only for UNIX systems.

+101
c colors std stdio
Jul 10 2018-10-10T00:
source share
7 answers

All modern terminal emulators use ANSI escape codes to show colors and other things.
Don’t worry about libraries, the code is really simple.

More info here .

Example in C:

#include <stdio.h> #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" int main (int argc, char const *argv[]) { printf(ANSI_COLOR_RED "This text is RED!" ANSI_COLOR_RESET "\n"); printf(ANSI_COLOR_GREEN "This text is GREEN!" ANSI_COLOR_RESET "\n"); printf(ANSI_COLOR_YELLOW "This text is YELLOW!" ANSI_COLOR_RESET "\n"); printf(ANSI_COLOR_BLUE "This text is BLUE!" ANSI_COLOR_RESET "\n"); printf(ANSI_COLOR_MAGENTA "This text is MAGENTA!" ANSI_COLOR_RESET "\n"); printf(ANSI_COLOR_CYAN "This text is CYAN!" ANSI_COLOR_RESET "\n"); return 0; } 
+249
Jul 10 2018-10-10T00:
source share

Working with color sequences can become erratic, and different color sequence indicators can be used on different systems.

I suggest you try ncurses . Besides color, ncurses can do many other neat things with a console interface.

+11
Jul 10 '10 at 13:50
source share

You can output special color management codes to get color output to the terminal, here is a good resource on how to print colors .

For example:

 printf("\033[22;34mHello, world!\033[0m"); // shows a blue hello world 

EDIT: My original uses color-code hints that don't work :( This is one (I tested it).

+8
Jul 10 '10 at
source share

You can assign one color to each function to make it more useful.

 #define Color_Red "\33[0:31m\\]" // Color Start #define Color_end "\33[0m\\]" // To flush out prev settings #define LOG_RED(X) printf("%s %s %s",Color_Red,X,Color_end) foo() { LOG_RED("This is in Red Color"); } 

As wise, you can choose different color codes and make it more general.

+6
Jul 10 '10 at 2:00
source share

If you use the same color for the entire program, you can define the printf() function.

  #include<stdio.h> #define ah_red "\e[31m" #define printf(X) printf(ah_red "%s",X); #int main() { printf("Bangladesh"); printf("\n"); return 0; } 
+2
Feb 01 '17 at 17:17
source share

Because you cannot print a character with string formatting. You might also consider adding a format with something like this.

 #define PRINTC(c,f,s) printf ("\033[%dm" f "\033[0m", 30 + c, s) 

f has a format like printf

 PRINTC (4, "%s\n", "bar") 

will print blue bar

 PRINTC (1, "%d", 'a') 

will print red 97

+1
Jun 04 '16 at 3:09 on
source share
 #include <stdio.h> #define BLUE(string) "\x1b[34m" string "\x1b[0m" #define RED(string) "\x1b[31m" string "\x1b[0m" int main(void) { printf("this is " RED("red") "!\n"); // a somewhat more complex ... printf("this is " BLUE("%s") "!\n","blue"); return 0; } 

reading wikipedia :

  • \ x1b [0m resets all attributes
  • \ x1b [31 m sets the foreground color to red
  • \ x1b [44m will set the blue background.
  • both: \ x1b [31; 44 m
  • both but inverted: \ x1b [31; 44; 7 m
  • do not forget to reset then \ x1b [0m ...
+1
Aug 21 '18 at 11:06
source share



All Articles