To change text color in C, print an ANSI escape code before your text: printf("\033[31mRed text\033[0m\n"); prints in red and then resets. That’s the whole trick — no library, no header, no Turbo C. The old way you’ll still find in most tutorials, textcolor() from <conio.h>, only ever worked in MS-DOS compilers and won’t compile with gcc or clang at all. ANSI escape codes work in every modern terminal: Linux, macOS, and Windows 10+ (Windows Terminal, PowerShell, and cmd all support them now). This page gives you a tested, warning-free C89 program with color macros you can paste straight into your own projects.
How It Works — Step by Step
- The escape sequence:
\033is the ESC character — decimal 27, written as the octal escape033.\033[tells the terminal “a formatting command follows” — the terminal interprets it instead of printing it. - The color code:
31mmeans foreground red;32mgreen;44mblue background. Codes combine with semicolons:\033[1;36mis bold cyan. - Reset when done:
\033[0mreturns the terminal to its defaults. Forget this and everything printed afterward — including your shell prompt — stays colored. - Macros keep it readable:
#define RED "\033[31m"lets you writeprintf(RED "error" RESET). Adjacent string literals are concatenated by the compiler, so no runtime cost.
| Color | Foreground | Background |
|---|---|---|
| Black | \033[30m |
\033[40m |
| Red | \033[31m |
\033[41m |
| Green | \033[32m |
\033[42m |
| Yellow | \033[33m |
\033[43m |
| Blue | \033[34m |
\033[44m |
| Magenta | \033[35m |
\033[45m |
| Cyan | \033[36m |
\033[46m |
| White | \033[37m |
\033[47m |
| Reset all | \033[0m |
|
| Bold | \033[1m |
|
C Program to Change Text Colors
/* Change text color in C with ANSI escape codes
* Compile: gcc -ansi -Wall -Wextra text_colors.c -o text_colors */
#include <stdio.h>
#define RESET "\033[0m"
#define BOLD "\033[1m"
#define RED "\033[31m"
#define GREEN "\033[32m"
#define YELLOW "\033[33m"
#define BLUE "\033[34m"
#define MAGENTA "\033[35m"
#define CYAN "\033[36m"
#define BLACK "\033[30m"
#define BG_WHITE "\033[47m"
int main(void)
{
printf(RED "This text is red\n" RESET);
printf(GREEN "This text is green\n" RESET);
printf(YELLOW "This text is yellow\n" RESET);
printf(BLUE "This text is blue\n" RESET);
printf(MAGENTA "This text is magenta\n" RESET);
printf(BOLD CYAN "This text is bold cyan\n" RESET);
printf(BG_WHITE BLACK "Black text on a white background\n" RESET);
printf("Back to the default color.\n");
return 0;
}
How to Compile and Run
gcc -ansi -Wall -Wextra text_colors.c -o text_colors
./text_colors
Compiles with zero warnings on gcc and clang. Works as-is on Linux, macOS, Windows Terminal, and PowerShell.
Sample Output
This text is red This text is green This text is yellow This text is blue This text is magenta This text is bold cyan Black text on a white background Back to the default color.
Each line appears in its named color in the terminal (colors can’t be shown in this plain-text block — run it and see). This is a real captured run of the exact code above.
Code Explanation
- Why
\033and not\e:\eis a gcc extension, not standard C — it fails under-ansi -Wall. The octal escape\033(or hex\x1b) is portable to every compiler. - String concatenation does the combining:
printf(BOLD CYAN "text" RESET)becomes one single string at compile time:"\033[1m\033[36mtext\033[0m". - Always reset: the terminal is stateful. Every colored print in this program ends with
RESETso no color leaks into the next line or your prompt. - What about
textcolor()andconio.h? They were Borland/Turbo C extensions for MS-DOS, never part of the C standard. Code using them won’t build with any modern compiler — if a tutorial showsclrscr()ortextcolor(RED), it’s showing you 1990s DOS code. - One honest caveat: if output is redirected to a file, the escape codes are written literally as bytes. Production tools call
isatty()to disable colors when not printing to a terminal — a good next exercise.
What This Program Teaches
- ANSI escape sequences — the universal terminal control language
- Compile-time string concatenation — adjacent literals merge into one
- Octal character escapes —
\033is ESC, and why\eisn’t portable - Stateful output devices — set, use, reset
Related C Programs
- ANSI Escape Codes in C — Cursor Control — hide/show the cursor and move it anywhere on screen
- Escape Characters in C — \n, \t, \033 and friends explained
- Pattern Programs in C — try printing them in color
Test yourself: our free C Programming Quiz app for Android has 150+ questions with explanations for every answer.
Recommended Book
The C Programming Language by Kernighan & Ritchie is where the character-escape rules used here are defined. We’ve solved all of the book’s exercises. Also on Amazon.com.