1

I want to make a text in color in C. I tried using some method below but this does not seem work on all platforms and C versions.

printf("\033[1;31m");

printf("Hello World\n");

printf("\033[0m");

On Windows 10 this seems to work ok, but on other systems I get this printed as text such as ←[1;31m or nothing prints at all.

Is there a more standard way to color a text which is more universal accross Windows and Unix systems?

S3DEV
  • 8,768
  • 3
  • 31
  • 42
  • 2
    Standard C has no standardized way to decorate text. Your best bet is to use ANSI sequences (I think that's what you are doing) and stick to basic features, but even on the same operating system the results depend on the terminal and probably even on the terminal settings. – Peter - Reinstate Monica Jan 25 '22 at 10:56
  • [Here](https://stackoverflow.com/a/33206814/3150802) is an answer with various examples. – Peter - Reinstate Monica Jan 25 '22 at 11:00
  • @Peter-ReinstateMonica - As demonstrated in the question, OP is using ANSI sequences. However the Win handle is not being obtained, therefore not working. Later Win10s do this under the hood, but previous versions do not. – S3DEV Jan 25 '22 at 11:00
  • @S3DEV I'm not sure why you would need a window handle to print text to standard out. – Peter - Reinstate Monica Jan 25 '22 at 11:01
  • 2
    Since "color" is a property of the used terminal, there is no standard way. Unix-like systems try to leverage this to some extent, but Windows is a different beast. Your best bet seems to be a library like ncurses. – the busybee Jan 25 '22 at 11:15

0 Answers0