I can't figure out how to change the color of text in C. I have googled and such but not really any explanations.
27 replies to this topic
#1
Posted 04 November 2010 - 12:59 PM
|
|
|
#3
Posted 04 November 2010 - 01:10 PM
What operating system? If MS-Windows you will want to use the Windows Console Functions, specifically WriteConsoleOutputAttribute
Hereis a nice tutorial that shows you how to change the color of the text.
Hereis a nice tutorial that shows you how to change the color of the text.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.
#4
Posted 04 November 2010 - 01:26 PM
If you are using Windows, this program will display "Hello World!" in bright pink:
#include <windows.h>
int main()
{
char msg[13] = "Hello World!";
//Message to be written
DWORD CharsWritten;
//An output value of the function used to write to the console
HANDLE conout; //An identifier for the console window's text
conout = CreateFile("CONOUT$",GENERIC_WRITE,0,NULL,3,FILE_ATTRIBUTE_NORMAL,NULL);
//Sets the identifier to point to the console's window. CreateFile is Windows's function
//for opening or creating a file
SetConsoleTextAttribute(conout,FOREGROUND_BLUE|FOREGROUND_RED|FOREGROUND_INTENSITY);
//Sets the color to a bright magenta
WriteConsole(conout,msg,12,&CharsWritten,NULL);
//Writes the message to the console
}
Latinamne loqueris?
#5
Posted 04 November 2010 - 01:27 PM
Yeah its windows, thanks for the links I will definitely read up. Is it possible for doing such a thing in Linux?
#6
Posted 04 November 2010 - 01:31 PM
Easily through ANSI terminal escapes, Dargueta has a nice tutorial here:
http://forum.codecal...ell-script.html
http://forum.codecal...ell-script.html
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#7
Posted 04 November 2010 - 01:33 PM
For Linux, although I do not know much on the matter, I think you would probably want to use something like ncurses.
EDIT: Or those escape sequences
EDIT: Or those escape sequences
Latinamne loqueris?
#8
Posted 05 November 2010 - 04:36 AM
I'm not sure how helpful I am, but it's easy using TurboC(Windows). Just include conio.h, and use the following functions -
You can either provide numbers, or use the pre-defined color constants -
You can use all colors with textcolor(), but only the black ones with textbackground().
After you used the above functions and set your desired colors, nothing will change if you'll use the standard(puts, putch, printf) output functions. You'll have to use the following functions -
They work the same as puts/printf, but are effected by the colors you set earlier. Here's a small example so you'll see what I'm talking about -
Quote
textcolor(int newcolor)
Quote
textbackground(int newcolor)
Quote
BLACK (0)
BLUE (1)
GREEN (2)
CYAN (3)
RED (4)
MAGENTA (5)
BROWN (6)
LIGHTGRAY (7)
DARKGRAY (8)
LIGHTBLUE (9)
LIGHTGREEN (10)
LIGHTCYAN (11)
LIGHTRED (12)
LIGHTMAGENTA (13)
YELLOW (14)
WHITE (15)
BLUE (1)
GREEN (2)
CYAN (3)
RED (4)
MAGENTA (5)
BROWN (6)
LIGHTGRAY (7)
DARKGRAY (8)
LIGHTBLUE (9)
LIGHTGREEN (10)
LIGHTCYAN (11)
LIGHTRED (12)
LIGHTMAGENTA (13)
YELLOW (14)
WHITE (15)
After you used the above functions and set your desired colors, nothing will change if you'll use the standard(puts, putch, printf) output functions. You'll have to use the following functions -
Quote
cprintf();
cputs();
cputs();
Quote
#include <stdio.h>
#include <conio.h>
int main()
{
clrscr();//Clears the screen.
textcolor(RED);//Makes the text red.
textbackground(BLUE);//Makes the text's background blue.
puts("This is a string");//Not effected.
cputs("This is another string");//The text will be red with blue background.
getch();//Pauses the program till a character is pressed.
return 0;
}
#include <conio.h>
int main()
{
clrscr();//Clears the screen.
textcolor(RED);//Makes the text red.
textbackground(BLUE);//Makes the text's background blue.
puts("This is a string");//Not effected.
cputs("This is another string");//The text will be red with blue background.
getch();//Pauses the program till a character is pressed.
return 0;
}

There is no problem that cannot be solved by the use of high explosives.
#9
Posted 05 November 2010 - 05:28 PM
It's probably best to use the Windows API, because the old functions for DOS are slower. You can use the same color codes, so that won't be a problem.
EDIT: Removed the false information. Sorry!
EDIT: Removed the false information. Sorry!
Edited by mebob, 05 November 2010 - 08:15 PM.
Latinamne loqueris?
#10
Posted 05 November 2010 - 05:31 PM
mebob said:
It's probably best to use the Windows API, because the old functions for DOS are much slower, and probably won't be supported in Windows forever. You can use the same color codes, so that won't be a problem.
They work directly with video modes IIRC, there is nothing to really discontinue.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
#11
Posted 05 November 2010 - 05:36 PM
Use the escape sequence \e
ex:
\e[31m makes the text red.
\e[32m makes the text green.
\e[3xm makes the text some other color.
\e[0m stops coloring text.
printf( "\e[31mHello World\e[0m\n" ); prints "Hello world" in red.
ex:
\e[31m makes the text red.
\e[32m makes the text green.
\e[3xm makes the text some other color.
\e[0m stops coloring text.
printf( "\e[31mHello World\e[0m\n" ); prints "Hello world" in red.
Programming is a journey, not a destination.
#12
Posted 05 November 2010 - 09:36 PM
DarkLordofthePenguins said:
Use the escape sequence \e
There is only \e in BASH, not in ANSI C. You must use \033 or equivalent.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









