hello world i would like to know to print on terminal a message with color and backround without external library i find this on internet and it works pretty fine but i would like to coloriza backround too!!

#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;
}

how can i change backround color ?

Dani AI

Generated

As already discovered, the terminal understands ANSI SGR (Select Graphic Rendition) sequences. To set a background color use the SGR parameters rather than only the foreground ones:

  • Standard backgrounds: 40–47
  • Bright backgrounds: 100–107
  • 256-color palette background: 48;5;N (N = 0..255)
  • Truecolor background: 48;2;R;G;B (R,G,B = 0..255)

Combine foreground and background by separating codes with semicolons and terminate the sequence with m. was right to point you at ANSI sequences — below are concrete examples to try (print them to a terminal, not to a log file):

/* examples */
printf("\x1b[41mRed background only\x1b[0m\n");
printf("\x1b[1;97;41mBold bright white on red background\x1b[0m\n");
printf("\x1b[48;5;196m256-color background (palette 196)\x1b[0m\n");
printf("\x1b[48;2;255;128;0mTruecolor orange background\x1b[0m\n");

Practical notes and troubleshooting:

  • These codes color only the characters you print. To make a whole line look like a solid background, print a line of spaces with the background color and then reset.
  • Avoid emitting escapes when output is redirected: check isatty(fileno(stdout)) and honor conventions like NO_COLOR or TERM.
  • Not every terminal supports 256-color or truecolor. If you need portability, query termcap/terminfo (or use tput/ncurses) to obtain the correct capability (setab) instead of hard-coding escapes.
  • On Windows consoles you may need to enable ANSI handling or fall back to the Win32 console API.

If you want a drop-in, portable solution later, consider a small terminfo-based helper or a tiny color wrapper; for quick, local use the raw escapes above will do.

So far, so good. Try some more web searching, especially studying the ANSI terminal control sequences, which provide just what you need. FWIW, I don't give obvious answers to questions you can deal with yourself with a very little effort!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.