Table of Contents
This updated guide examines Include: How to Get Color in C Program and organizes the essential facts, background, and practical takeaways in clear American English.
Part 1
Changing Output Text Color
Include the Standard Input and Output library. This common library allows you to change the color that the text output displays. Add the following code to the top of your program:[1]XResearch source
#include
Include the Console Input and Output library. This will make it easier to capture keyboard input from the user. Add the library below thestdio.hlibrary:
#include#include
Use thetextcolorfunction to define what color you want to use for text. You can use this function to vary the text colors of your output. Colors must be written in all caps, or expressed as a numeral:
#include#includemain(){textcolor(RED);// You could type "4" instead of "RED", but it is not as readable}ColorNumerical ValueBLACKBLUE1
GREEN2
CYAN3
RED4
MAGENTA5
BROWN6
LIGHTGRAY7
DARKGRAY8
LIGHTBLUE9
LIGHTGREEN10
LIGHTCYAN11
LIGHTRED12
LIGHTMAGENTA13
YELLOW14
WHITE15
- There are more colors than this. The colors available depend on the installed graphics drivers and current mode. Colors must be written in all caps.[2]XResearch source
Add output text and finish the program. Include acprintffunction to display some text in your new color. Use agetchfunction at the end to close the program when the user presses a key.
#include#includemain(){textcolor(RED);// You could type "4" instead of "RED", but it is not as readablecprintf("Hello, World!");getch();return0;}
Part 2
Changing Drawing Color
Include the graphics library. The C graphics library allows you to draw objects, as well as adjust their color. You can get access to the graphics library by including it at the top of your program:
#include
Include the Console Input and Output library. You can use this library to easily capture a user's input. Add the library below thegraphics.hlibrary:
#include#include
Initialize the variables for the graphics driver and mode. You'll need to do this before you begin drawing objects, so that the program has access to the system graphics drivers. This will create an area on the screen that the object will be drawn on.
#include#includemain(){intgd=DETECT,gm;initgraph(&gd,&gm,"C:TCBGI");// Change this to the path of your compiler}
Set the color of the object you want to draw. Before coding in an object, use thesetcolorfunction to define the color of the object you are about to draw:[3]XResearch source
#include#includemain(){intgd=DETECT,gm;initgraph(&gd,&gm,"C:TCBGI");setcolor(BLUE);// You can enter "1" instead of "BLUE" to get the same color, but this is not as readable}
Draw an object of your choice. For this example, you'll be drawing a rectangle using therectanglefunction. You can use any of thegraphics.hdrawing tools to draw in the color that you set.
#include#includemain(){intgd=DETECT,gm;initgraph(&gd,&gm,"C:TCBGI");setcolor(BLUE);rectangle(50,50,100,100);// These numbers indicate the location of the left-top and right-bottom corners}
Finish off the program and test it. Add thegetchcommand and turn off the graphics area as you close the program. Compile it and give it a test run.
#include#includemain(){intgd=DETECT,gm;initgraph(&gd,&gm,"C:TCBGI");setcolor(BLUE);rectangle(50,50,100,100);getch();closegraph();return0;}
FAQ
What is Include: How to Get Color in C Program about?
It provides a structured overview of include, explains the main context, and highlights practical takeaways for readers.
Why does this topic matter?
Understanding the main concepts helps readers evaluate the issue, avoid common mistakes, and make better-informed decisions.
How should readers use this information?
Use the guidance as a practical starting point, confirm details that may have changed, and follow current product, safety, or security recommendations.
Reader Comments 0
Sign in with email or Google to join the discussion.