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

Dani AI

Generated

The visible problem is that calling system("color ...") runs cmd's built-in color command, which changes the console's palette/attributes for the whole screen — including text already printed. That is why the first line ends up recolored when the second system("color c") runs. As pointed out, printing is sequential; to keep each line's own color, change the console attribute around each write rather than calling the global color command.

A reliable Windows approach is to use the Console API: get the standard output handle, save the current attributes, call SetConsoleTextAttribute() before each write, then restore the saved attributes. Example (works in a real cmd.exe console):

#include <stdio.h>
#include <windows.h>

int main(void)
{
    HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    WORD orig = 0;
    if (GetConsoleScreenBufferInfo(h, &csbi)) orig = csbi.wAttributes;

    SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY); // yellow
    puts("An enemy appears");

    SetConsoleTextAttribute(h, FOREGROUND_RED | FOREGROUND_INTENSITY); // light red
    puts("This is an apple.");

    SetConsoleTextAttribute(h, orig);
    return 0;
}

Notes and caveats: do not rely on <conio.h> for portability — many compilers/IDE output panes do not implement color functions. Visual Studio's Output window is not a real console and will not show colors; run the program from cmd.exe. Windows 7 does not natively support ANSI escape sequences (modern Windows 10+ can when virtual-terminal processing is enabled), so the Console API above is the most compatible choice for the environment mentioned. As recommended, prefer the Console API rather than repeated system("color ...") calls.

Recommended Answers

All 4 Replies

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.