Text Color on C Programming

Asked By 10 points N/A Posted on -
qa-featured

What can I do to change the default color of the Text on C programming, (this should appear in the output window)?

SHARE
Best Answer by Emon Asejo
Best Answer
Best Answer
Answered By 0 points N/A #102464

Text Color on C Programming

qa-featured

Hello Edson Chua,

All you need to make sure is to add the <windows.h> header for the script color to change as your need. Below is the snippet you can try with entering different values of c between 1 to 255 and suite yourself for the new look of your text appearance.

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

int main() {
    HANDLE clr;
    int c=5;            // the value of c can be from 1 to 255
    clr=GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(clr, c);
    printf("%3d %sn", c, "You can choose from 1 to 255 for the variation of text color");
    getchar();
    return(0);
}

Hope this will help you to solve your problem. Thank you

Emon Asejo

Answered By 0 points N/A #102463

Text Color on C Programming

qa-featured

You need to use Windows API for the purpose

In order that you do it for Linux, the solution will not work. Only versions of C which support Windows API function can do the job.

Try the following C program

#include <stdio.h>

#inlclude<windows.h>

int main(){

HANDLE hand;                //CREATE A HANDLE FOR THE STANDARD OUTPUT.

int i=100;    //vary fro m 1 to 255

hand=GetStdHandle(STD_OUTPUT_HANDLE);                       //ASSIGN HANDLE TO THE CORRECT STD_OUTPUT

SetConsoleTextAttribute ( hand, i);                            //ASSIGNING A COLOR DESCRIBED BY 'i' TO BE DISPLAYED. 

printf(“%3d %sn”, i, ”Greate color”);                 //PRINT A TEXT SHOWING COLOR.

getchar();

return(0);

}

You can vary i from 1 to 255 to obtain different colors for the text being printed. This is amazing. Is not it?

You can change the color of code too in Linux. Try it.

Thanks.

Answered By 10 points N/A #102465

Text Color on C Programming

qa-featured

That's it Edmon. You got it. Thanks on that help! Got changes on the default text color because of your comment. Thank you techyv!@@

Related Questions