C Online Compiler
Example: ANSI Background Color Example in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// ANSI Background Color Example #include <stdio.h> // ANSI Escape Codes for Text Background Colors #define ANSI_COLOR_BLACK_BG "\x1b[40m" #define ANSI_COLOR_RED_BG "\x1b[41m" #define ANSI_COLOR_GREEN_BG "\x1b[42m" #define ANSI_COLOR_YELLOW_BG "\x1b[43m" #define ANSI_COLOR_BLUE_BG "\x1b[44m" #define ANSI_COLOR_MAGENTA_BG "\x1b[45m" #define ANSI_COLOR_CYAN_BG "\x1b[46m" #define ANSI_COLOR_WHITE_BG "\x1b[47m" // Light gray background // ANSI Escape Codes for Text Foreground Colors (optional, for contrast) #define ANSI_COLOR_BLACK "\x1b[30m" #define ANSI_COLOR_WHITE "\x1b[37m" // ANSI Escape Code to Reset all attributes (colors, bold, etc.) #define ANSI_COLOR_RESET "\x1b[0m" int main() { // Step 1: Display text with a red background and white foreground printf("%s%sThis text has a red background and white foreground.%s\n", ANSI_COLOR_RED_BG, ANSI_COLOR_WHITE, ANSI_COLOR_RESET); // Step 2: Display text with a green background and default foreground printf("%sThis text has a green background.%s\n", ANSI_COLOR_GREEN_BG, ANSI_COLOR_RESET); // Step 3: Display text with a blue background and black foreground printf("%s%sHello from a blue background!%s\n", ANSI_COLOR_BLUE_BG, ANSI_COLOR_BLACK, ANSI_COLOR_RESET); // Step 4: Display text with a yellow background printf("%sWarning: Important information here.%s\n", ANSI_COLOR_YELLOW_BG, ANSI_COLOR_RESET); // Step 5: Demonstrate a default background (by resetting) printf("This text has the default background and foreground.\n"); return 0; }
Output
Clear
ADVERTISEMENTS