How to change text color in windows console in C

1 Answer

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

#define DEFAULT_COLOR 7

void setColor(int color) {
	HANDLE hConsole;
	hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hConsole, color);
}

int main()
{
	setColor(FOREGROUND_BLUE);
	printf("This text is FOREGROUND_BLUE\n");

	setColor(DEFAULT_COLOR);
	printf("This text is DEFAULT_COLOR\n");

	return 0;
}



/*
run:

This text is FOREGROUND_BLUE
This text is DEFAULT_COLOR

*/

 



answered Nov 26, 2021 by avibootz

Related questions

1 answer 213 views
1 answer 204 views
1 answer 230 views
1 answer 223 views
2 answers 243 views
1 answer 215 views
...