Jump to content

Color in C?

- - - - -

  • Please log in to reply
27 replies to this topic

#1
JanitorMoe

JanitorMoe

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
I can't figure out how to change the color of text in C. I have googled and such but not really any explanations.

#2
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
You need OS specific functions. If you are using Windows, you need to use functions such as WriteConsole(). To read up on the functions for Windows, go here.
Latinamne loqueris?

#3
Ancient Dragon

Ancient Dragon

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 400 posts
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.
Visit Grandpa's Forums, a social networking forum, with family-oriented arcade games, blogs, discussion forums, and photo albums.

#4
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
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
JanitorMoe

JanitorMoe

    Learning Programmer

  • Members
  • PipPipPip
  • 54 posts
Yeah its windows, thanks for the links I will definitely read up. Is it possible for doing such a thing in Linux?

#6
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
Easily through ANSI terminal escapes, Dargueta has a nice tutorial here:
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.

#7
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
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
Latinamne loqueris?

#8
AdvMutant

AdvMutant

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 438 posts
I'm not sure how helpful I am, but it's easy using TurboC(Windows). Just include conio.h, and use the following functions -

Quote

textcolor(int newcolor)

Quote

textbackground(int newcolor)
You can either provide numbers, or use the pre-defined color constants -

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)
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 -

Quote

cprintf();
cputs();
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

#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;
}

Posted Image
There is no problem that cannot be solved by the use of high explosives.


#9
mebob

mebob

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 490 posts
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!

Edited by mebob, 05 November 2010 - 08:15 PM.

Latinamne loqueris?

#10
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

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.

#11
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
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.
Programming is a journey, not a destination.

#12
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200

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.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users