How to change the color of a console line in C?

Asked

Viewed 1,276 times

-2

so I’d like to know how do I change the color of that line... It’s a college project, and I’ve done research on other forums and everything, but I can never get an answer that works. I’m using the compiler Devc++ (5.11). When I say change the color I mean change both a single character and a string whole or one vector.

#include <stdio.h>

void main (){


    printf("Ola bom dia!");


    system("pause");
}
  • 1

    give a look here: https://stackoverflow.com/questions/3219393/stdlib-and-colored-output-in-c

1 answer

3


You can check the ANSI exhaust codes, then use these codes in the printf.

Thus:

#include <stdio.h>

#define VERMELHO     "\x1b[31m"
#define VERDE   "\x1b[32m"
#define AZUL    "\x1b[34m"
#define RESET   "\x1b[0m"

int main () 
{
    printf(VERMELHO     "Texto em vermelho"     RESET "\n");
    printf(VERDE   "Texto em verde"   RESET "\n");
    printf(AZUL    "Texto em azul"    RESET "\n");

    return 0;
}
  • So, I was able to change the color in an online compiler, but not in Devc++ and as the project is to be run in this compiler, there would be some other way to be done?

  • 4

    The point is that this standardized way of changing colors on terminals works on any operating system except Windows. microsoft has chosen not to evolve its command line interface since the time of DOS/Windows 3, ~1990; - so these ANSI commands are still not standard in windows today

  • 4

    The solution is to search for "ansi color codes windows" and check the options - among alternative programs on the terminal or settings to enable ANSI colors. Using "within the IDE" should not be a concern for you - but rather, operating on the terminal.

  • 3

    @Paulohenrique You are confusing some concepts. " the project is to be run on this compiler" - The compiler is to compile not run, and is made based on something you already have on your system like gcc, g++, Clang, etc, and not based on the IDE you use. Devc++ is an IDE.

  • 1

    Yes - that code will compile on any eater. What can’t be confused is that the IDE, which has the code editor, calls the compiler itself, and has a built-in terminal emulator with "where the program runs" - the IDE’s terminal emulator is just to avoid having to switch windows to see prints and other things - has no importance to what the program actually does.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.