C Online Compiler
Example: Windows Console Color Example in C
C
C++
C#
Java
Python
PHP
main.c
STDIN
Run
// Windows Console Color Example #include <stdio.h> #include <windows.h> // Required for Windows API functions int main() { // Step 1: Get a handle to the standard output console HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); // Step 2: Store original console attributes to restore later CONSOLE_SCREEN_BUFFER_INFO consoleInfo; GetConsoleScreenBufferInfo(hConsole, &consoleInfo); WORD originalAttrs = consoleInfo.wAttributes; // Step 3: Set text color to green (FOREGROUND_GREEN) SetConsoleTextAttribute(hConsole, FOREGROUND_GREEN | FOREGROUND_INTENSITY); printf("This text is green and bright!\n"); // Step 4: Set text color to red (FOREGROUND_RED) and background to yellow (BACKGROUND_YELLOW) SetConsoleTextAttribute(hConsole, FOREGROUND_RED | BACKGROUND_YELLOW); printf("This text is red with a yellow background!\n"); // Step 5: Set text color to blue with high intensity SetConsoleTextAttribute(hConsole, FOREGROUND_BLUE | FOREGROUND_INTENSITY); printf("This is a bright blue informational message.\n"); // Step 6: Restore original console attributes before exiting SetConsoleTextAttribute(hConsole, originalAttrs); printf("This text is back to default.\n"); return 0; }
Output
Clear
ADVERTISEMENTS