1

a bit of a newbie here. And this seems like a repeated question because others have asked the question. But when i tried the suggested answer it doesn't work for me and idk why. So i have to change the color of the output and these are what i found:

#include <stdio.h>
void red () {
printf("\033[1;31m");

}

void yellow ()
{
 printf("\033[1;33m");
}

void reset () {
  printf("\033[0m");
}

int main () {
  red();
  printf("Hello ");
  yellow();
  printf("world\n");
  reset;
  return 0;
}

which prints out

[1;31mHello [1;33mworld

as the answer, and i also found this

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

which prints out

[31mThis text is RED![0m
[32mThis text is GREEN![0m
[33mThis text is YELLOW![0m
[34mThis text is BLUE![0m
[35mThis text is MAGENTA![0m
[36mThis text is CYAN![0m

so it doesn't change any of the colors and just prints them out as they are... why does this happen and how do i color the output?..

  • You need to output to a console that accepts ANSI control codes. What terminal are you using? – Paul Hankin May 23 '20 at 10:20
  • @PaulHankin tbh idk lol. the output looks like just the regular windows command prompt and im using Dev-C++ to write the codes in. And if i can't see the colors myself i cant submit it as a project that im doing lol (trying to change the colors of numbers in minesweeper) – CurryFlurry May 23 '20 at 10:21
  • Does this answer your question? [How to make win32 console recognize ANSI/VT100 escape sequences?](https://stackoverflow.com/questions/16755142/how-to-make-win32-console-recognize-ansi-vt100-escape-sequences) – Paul Hankin May 23 '20 at 10:26
  • @PaulHankin yeah, so i have to either use external stuff or enable it correct? windows update ftw i guess they turned ansi off on the command prompt lol. thanks m8 – CurryFlurry May 23 '20 at 10:41
  • @CurryFlurry Just FYI, I tried your code on my Ubuntu Linux machine and it works great. The problem must be with the terminal. – CodeWash May 23 '20 at 11:24
  • Please tell us in which environment (OS, etc...) you are working. – Pierre François May 23 '20 at 12:43

0 Answers0