0

There's a way to change the output color at C ? for example:

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

I have this code which I tested, but the color has just shown at the vs code terminal. I'd like to know if there's a way to be also shown at cmd when I compile the binary code generated from the code.

  • 1
    The colour is part of the terminal capabilities, not of the compiled code. Apparently the "vs code terminal" has ANSI-compatible colour capability and the "cmd" does not. – pmg Sep 23 '21 at 13:51
  • It depends on the console that output is being sent to. In some terminals this will look like you wanted it to. In some it will appear as normal, uncoloured text. In some it will appear as garbage. – Cheatah Sep 23 '21 at 13:51
  • 1
    The Windows console mode can be changed by the `SetConsoleMode` function to interpret ANSI escape sequences, but I do not know if there is a built-in or external command to do that from the CMD shell in the current console, or if it gets turned off by the CMD shell between commands. – Ian Abbott Sep 23 '21 at 13:55
  • Apparently, the ANSI escape sequence support was added in Windows 10 TH2 (v1511) and works out of the box. See https://stackoverflow.com/a/35864976/5264491 and https://superuser.com/a/1050078 – Ian Abbott Sep 23 '21 at 14:07
  • However, the ANSI escape support in the console seems to get turned off by CMD when it runs another executable. (At least, it seems to be turned off when I run your program!) – Ian Abbott Sep 23 '21 at 14:22
  • 1
    Does this answer your question? [Using colors with printf](https://stackoverflow.com/questions/5412761/using-colors-with-printf) – Dominique Sep 23 '21 at 14:22

1 Answers1

0

Short answer is yes.

But it has nothing to do with C. It depends on the operating system you are using and the environment, like the type of console. Under Windows you have the console mode fully documented at Microsoft Docs.

Under Linux you can have these escape codes from the 90's, based on the Digital VT terminal series command codes. These commands are also available under recent versions of Windows as something like VT-Terminals so you can have something portable today on Linux Windows or MacOS.

Is is common under Linux to use curses library, originally from the Unix System V, now in the form of ncurses, also ported to Windows, to have some help in control of screen and colors

arfneto
  • 1,227
  • 1
  • 6
  • 13