Say i just do a simple printf statement
printf("i want this text to be different color > %s", thisbit); i want whatever i print out at the %s (the string) to be different color.
Im sure it only something little that why i ask.
Say i just do a simple printf statement
printf("i want this text to be different color > %s", thisbit); i want whatever i print out at the %s (the string) to be different color.
Im sure it only something little that why i ask.
Good summary so far — 's ANSI approach is exactly what most Linux terminals accept, and was right to flag Windows as a different case. For portability and reliability, avoid sprinkling raw escape bytes everywhere: detect whether stdout is a terminal, provide a small wrapper, and either emit ANSI (POSIX) or call the Win32 console API (Windows).
Basic portability checklist
isatty(fileno(stdout)); Windows: _isatty(_fileno(stdout))). Windows notes and example
SetConsoleTextAttribute) is the safe way to change colors. On modern Windows 10+ you can enable Virtual Terminal Processing so the console understands ANSI escapes; otherwise use the console attribute API and restore the original attributes when done.Example: enable ANSI processing (Windows 10+) — call once before printing ANSI sequences
#include <windows.h>
int enable_vt_mode(void) {
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (hOut == INVALID_HANDLE_VALUE) return 0;
DWORD mode = 0;
if (!GetConsoleMode(hOut, &mode)) return 0;
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif
mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
return SetConsoleMode(hOut, mode) != 0;
} Fallback: use GetConsoleScreenBufferInfo / SetConsoleTextAttribute to change and then restore attributes
#include <windows.h>
WORD save_console_attrs(void) {
CONSOLE_SCREEN_BUFFER_INFO csbi;
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
if (!GetConsoleScreenBufferInfo(hOut, &csbi)) return 0;
return csbi.wAttributes;
}
void set_console_attr(WORD attr) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), attr);
} Troubleshooting: if colors don't appear, confirm the terminal supports them, check TERM/terminal emulator settings, ensure output is not redirected, and always restore console attributes so the shell is not left colored. For more robust handling across many terminals, consider a small terminfo/ncurses-based layer or an existing cross-platform library.
Never mind find out.
For anyone else who was curious. Type one of these before the text you want to
right.
\033[22;30m - black
\033[22;31m - red
\033[22;32m - green
\033[22;33m - brown
\033[22;34m - blue
\033[22;35m - magenta
\033[22;36m - cyan
\033[22;37m - gray
\033[01;30m - dark gray
\033[01;31m - light red
\033[01;32m - light green
\033[01;33m - yellow
\033[01;34m - light blue
\033[01;35m - light magenta
\033[01;36m - light cyan
\033[01;37m - white
e.g. for light blue
printf(" \033[01;34m Hello World"); The codes shown above this, require that your system has the ANSI.sys driver loaded. (shades of DOS!)
I don't have it on my WindowsXP, system, so it won't work for me.
If you're working or writing code for a Windows system, I strongly suggest you use the Windows API for this, instead.
I do all my programming on Linux :) worked fine for me :P
Then your distro has a version of ansi.sys installed. This was heavily used in the early days of DOS. I used it also.
I'll have to see if I can install it into my WindowsXP system, although Windows does have it's own API for this. I believe it's called SetConsoleTextColor().
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.