Change variable text colors inside printf


EFX

Joined
Mar 27, 2022
Messages
2
Reaction score
0
#define RED "\x1b[0;31m"
#define YELLOW "\x1b[0;33m"
#define RESET "\x1b[0m"

printf(RED"This text is red. \n"RESET); //This code works OK

The code below is what I want to do but it doesn't work. I want to change the text color based on the string variable color.

if (var > 5)
strcpy(color,RED);
else if (var > 3)
strcpy(color,YELLOW);
else
strcpy(color,RESET);

printf("%s My text to change color. \n",color); //the text color doesn't change
 
Ad

Advertisements

EFX

Joined
Mar 27, 2022
Messages
2
Reaction score
0
The above code does work after all. The error was unrelated to the code above. I'm sorry if anyone spent too much time finding another solution.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top