Notification texts go here Contact Us Buy Now!

stdlib and colored output in C

```html

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

Dealing with colour sequences can get messy and different systems might use different Colour Sequence Indicators.

I would suggest you try using ncurses. Other than colour, ncurses can do many other neat things with console UI.

You can assign one color to every functionality to make it more useful.



#define ColorRed "\033[31m // Color Start
#define ColorEnd "\033[0m // To flush out prev settings
#define LOG(RED) printf("%s %s %s",ColorRed,RED,ColorEnd)

foo()
{

LOG("This is in Red Color");

}

Likewise you can select different color codes and make this more generic


All modern terminal emulators use ANSI escape codes to show colours and other things.
Don't bother with libraries the code is really simple.

More info is here.


#include <stdio.h>

#define ANSI_COLOR_RED     "\033[31m"
#define ANSI_COLOR_GREEN   "\033[32m"
#define ANSI_COLOR_YELLOW  "\033[33m"
#define ANSI_COLOR_BLUE    "\033[34m"
#define ANSI_COLOR_MAGENTA "\033[35m"
#define ANSI_COLOR_CYAN    "\033[36m"
#define ANSI_COLOR_RESET   "\033[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;

}

Because you can print a character with string formating You can also think of adding a format with something like this


#define PRINT(c, s) printf("%dm" f "\n", c, s) 

is format as in printf


PRINT(%d, "bar")

will print 'blue bar'


PRINT(%d, 'c')

will print 'red c'

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


#include <stdio.h>
#define ahred "\033[31m"
#define printf(f, ...) printf(ahred "%s", f)
int main()
{

printf("Bangladesh");
printf("\n");
return 0;

}

reading Wikipedia:

  • ESC[0m resets all attributes
  • ESC[31m sets foreground color to red
  • ESC[44m would set the background to blue.
  • both :ESC[31;44m
  • both but inversed :ESC[31;44;1m
  • remember to reset afterwards :ESC[0m ...

Expanding on the answer of @AndrejsCain here is a code that works for 24bits rgb code (both for font and background color) with terminals that support such code (e.g. Konsole and iTerm as well as all libte based terminals including GNOME Terminal according to https://en.wikipedia.org/wiki/ANSI_escape_code#Colors)


#include <stdio.h>

#define ANSI_FONT_COL_RESET     "\033[0m"
#define FONT_COL_CUSTOM_RED     "\033[38;5;196m" // where rgb in \033[38;5;rrr;ggg;bbbm can go from 0 to 255 соответственно
#define FONT_COL_CUSTOM_GREEN   "\033[38;5;46m" // where rgb in \033[38;5;rrr;ggg;bbbm can go from 0 to 255 соответственно
#define FONT_COL_CUSTOM_BLUE    "\033[38;5;27m" // where rgb in \033[38;5;rrr;ggg;bbbm can go from 0 to 255 соответственно
#define BCKGR_COL_CUSTOM_RED   "\033[48;5;196m" // where rgb in \033[48;5;rrr;ggg;bbbm can go from 0 to 255 соответственно
#define BCKGR_COL_CUSTOM_GREEN "\033[48;5;46m" // where rgb in \033[48;5;rrr;ggg;bbbm can go from 0 to 255 соответственно
#define BCKGR_COL_CUSTOM_BLUE  "\033[48;5;27m" // where rgb in \033[48;5;rrr;ggg;bbbm can go from 0 to 255 соответственно

int main (int argc, char const *argv[]) {

  printf(FONT_COL_CUSTOM_RED     "This font color is CUSTOM_RED"           ANSI_FONT_COL_RESET "\n");
  printf(FONT_COL_CUSTOM_GREEN   "This font color is CUSTOM_GREEN"         ANSI_FONT_COL_RESET "\n");
  printf(FONT_COL_CUSTOM_BLUE    "This font color is CUSTOM_BLUE"          ANSI_FONT_COL_RESET "\n");
  printf(BCKGR_COL_CUSTOM_RED   "This background color is CUSTOM_RED"     ANSI_FONT_COL_RESET "\n");
  printf(BCKGR_COL_CUSTOM_GREEN "This background color is CUSTOM_GREEN"   ANSI_FONT_COL_RESET "\n");
  printf(BCKGR_COL_CUSTOM_BLUE  "This background color is CUSTOM_BLUE"    ANSI_FONT_COL_RESET "\n");
  printf(FONT_COL_CUSTOM_GREEN BCKGR_COL_CUSTOM_RED "This font color is CUSTOM_GREEN with background CUSTOM_RED"    ANSI_FONT_COL_RESET "\n");
  printf("This font color is NORMAL!\n");

  return 0;

}

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.