How to put a color in ONLY one variable in C?

Asked

Viewed 973 times

-1

How can I put a color in ONLY one variable of my code in C?

I could only change all the colors of ALL of the letters with system("color 02"); with the library #include <conio.h>.

But how do I have multiple colors on the same screen?

For example, how would you put each color name there with its own color on the same screen?

Edit: even if I put several system color 02 or textcolor it changes only once the color of the letter and does not allow multiple colors...

printf("\t\t================ MENU ==================\n");
printf("\n\t\t\tDigite 1 para a cor verde\n");
printf("\t\t\tDigite 2 para a cor azul\n");
printf("\t\t\tDigite 3 para a cor amarelo\n");
printf("\t\t\tDigite 4 para a cor vermelho\n");
printf("\t\t\tDigite 5 para a cor roxo\n");
printf("\t\t\tDigite 6 para a cor marrom\n\n");
printf("\t\t================ MENU ==================\n");

1 answer

0

Try this:

#include <stdio.h>

#define RED   "\x1B[31m"
#define GRN   "\x1B[32m"
#define YEL   "\x1B[33m"
#define BLU   "\x1B[34m"
#define MAG   "\x1B[35m"
#define CYN   "\x1B[36m"
#define WHT   "\x1B[37m"
#define RESET "\x1B[0m"

int main()
{
  printf(RED "red\n" RESET);
  printf(GRN "green\n" RESET);
  printf(YEL "yellow\n" RESET);
  printf(BLU "blue\n" RESET);
  printf(MAG "magenta\n" RESET);
  printf(CYN "cyan\n" RESET);
  printf(WHT "white\n" RESET);

  return 0;
}
  • It didn’t work, compiled but the letters get strange characters in their place

  • This works if your Windows is younger than 10 Threshold 2 (or if it’s Unix, of course), otherwise you have to use the Windows console API

Browser other questions tagged

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