I wish to change the color of my printf statement. I use following code:

#include <stdio.h>
#include <stdlib.h>

void main()
{
	system("color e");
	printf("An enemy appears\n");

	system("color c");
	printf("This is an apple.\n");
}

I wish to print the statement which is "An enemy appears" in yellow color and "This is an apple" in red color at SAME time.
But the output isn't what I want.
It just print out "An enemy appears" and "This is an apple" in red color.
Is that the "system" problem?
So how can i print 2 different color of stirngs in same page??

You should know you can't print two things, at exactly the same time.

How you change colors for your text, depends on your operating system. For windows, you should probably use the Windows API for this. If your compiler supports conio.h, you might be able to use that. If your system has ansi.sys compatibility (and the ansi.sys driver is loaded), then you might be able to use it's way to color the text color. For linux user's, NCURSES has it's way of coloring text.

The list goes on...;)

In other words, more info is needed about your OS.

my os is windows 7, and I think it is support conio.h
So how could I use conio.h to edit my color?

Please double check that you can use conio.h, because it's an older program, (mine is 16 bit, for instance), and unless it's been re-compiled, won't run on Windows 7 (will run on WindowsXP I know).

my os is windows 7, and I think it is support conio.h
So how could I use conio.h to edit my color?

Even if your compiler has <conio.h> in some form or another, I doubt you'll find anything like you want in it.

Hence I'd suggest that you forget about <conio.h> and instead use Windows API, i.e. SetConsoleTextAttribute(). Have a look at a small example.

This article has been dead for over six months. Start a new discussion instead.