0

I want to print a colored sentence, My code:

#include<stdio.h>


int main()
{
    printf("\033[0;34m");
    printf("This is Blue"); 
    return 0;
}

And this is output:

[0;34mThis is Blue

How can I fix this problem?

Pouria
  • 39
  • 8
  • 4
    What compiler/terminal are you using? – Fiddling Bits Jul 13 '20 at 17:22
  • The terminal is responsible to display colors. Which terminal are you using? Windows terminals don't handle colors for instance (both cmd and powershell). – Matthieu Jul 13 '20 at 17:23
  • 4
    If it's windows see [How to make win32 console recognize ANSI/VT100 escape sequences?](https://stackoverflow.com/questions/16755142/how-to-make-win32-console-recognize-ansi-vt100-escape-sequences) – fcdt Jul 13 '20 at 17:23
  • An usual terminal does not need to provide the ability to do special colored output. If, it is due to a specific terminal. Also: Why do you think `"\033[0;34m"` is displaying anything blue, too? Did you seen it anywhere? More information please. – RobertS supports Monica Cellio Jul 13 '20 at 17:24
  • with the right terminal your program does the expected result, for instance an LX terminal under raspbian – bruno Jul 13 '20 at 17:27
  • 2
    Working as expected using `gcc` and `Cygwin`/`bash`. – Fiddling Bits Jul 13 '20 at 17:28
  • 1
    Windows console can handle colours, but only via the [console API](https://learn.microsoft.com/en-us/windows/console/setconsoletextattribute). – Weather Vane Jul 13 '20 at 17:56
  • Your code seems to be working fine in VSCode in-built terminal. – Rohan Bari Jul 13 '20 at 18:17
  • 1
    Does this answer your question? [How to make win32 console recognize ANSI/VT100 escape sequences?](https://stackoverflow.com/questions/16755142/how-to-make-win32-console-recognize-ansi-vt100-escape-sequences) – Matthieu Jul 13 '20 at 19:09
  • You could also avoid the ANSI/VT codes by using a curses library (line ncurses or pdcurses). – Brecht Sanders Jul 13 '20 at 20:49
  • Welcome to SO. SO works best if there is feedback from the authors of questions. Especially if there are questions about missing details in comments. – Gerhardh Jul 14 '20 at 10:45

1 Answers1

0

Your program does exactly what it is supposed to do: output what you tell it to to the console.

However, if you are expecting to see colors when sending ANSI/VT100 codes to the console the console must understand these codes.

You didn't mention what operating system and console you are using.

If it's Windows for example the console doesn't support ANSI/VT100 codes by default (to enable see: https://learn.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences?redirectedfrom=MSDN).

If it Unix or Linux you may need to check what the TERM environment variable is set to and if that is supported by the console application you're using.

If you are using a remote console (like PuTTY on Windows) you may need to set the terminal type to xterm-color. enter image description here

Brecht Sanders
  • 6,215
  • 1
  • 16
  • 40