color text in C programming

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

43 Replies - 35953 Views - Last Post: 01 March 2010 - 02:07 PM Rate Topic: -----

#1 paakfaat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 04-August 08

color text in C programming

Post icon  Posted 04 August 2008 - 08:30 PM

I 'm using DEV-C++ to write my c program. And also I want to change the text color in the program in windows console environment. From my knowledge, I know that we can change 15 colors by using
SetConsoleTextAttribute (GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_RED); 

However I need more colors. I heard that curses can do it. But in DEV-C++, there are not <curses.h> header file. And I don't really understand how to use it or if it can help me to use with more colorful text color. Also, I tried to download ncurses 5.6 and curses 1.23. However, I don't know if they are useful or not. And I don't know how to use them.
So, please help me to answer the question.
And I need a example program which can be compiled in DEV-C++. Also, I'm using windows but not unix or linux. And my knowledge is very limited, please give me more details about it. Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: color text in C programming

#2 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5226
  • View blog
  • Posts: 27,000
  • Joined: 10-May 07

Re: color text in C programming

Posted 04 August 2008 - 08:40 PM

Have you tried using escape characters? I believe these will give you 16 bit color, as well as bold, blinking, & underline effects.

#include <stdio.h>
#include<iostream.h>
#include<conio.h>

int main(void) {
  cout << "regular " << '\033' << '[' << "boldface" << endl;
}


Was This Post Helpful? 1

#3 paakfaat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 04-August 08

Re: color text in C programming

Posted 04 August 2008 - 09:05 PM

But I'm using C language. I don't know how to use C++. Anyway, thanks for you suggestion.
Was This Post Helpful? 0
  • +
  • -

#4 no2pencil  Icon User is online

  • Toubabo Koomi
  • member icon

Reputation: 5226
  • View blog
  • Posts: 27,000
  • Joined: 10-May 07

Re: color text in C programming

Posted 04 August 2008 - 09:08 PM

View Postpaakfaat, on 5 Aug, 2008 - 12:05 AM, said:

But I'm using C language.


Oops, my bad. So you replace the cout with printf.

Maybe this will help you out just a bit more :

#include <stdio.h>

#define RESET           0
#define BRIGHT          1
#define DIM             2
#define UNDERLINE       3
#define BLINK           4
#define REVERSE         7
#define HIDDEN          8

#define BLACK           0
#define RED             1
#define GREEN           2
#define YELLOW          3
#define BLUE            4
#define MAGENTA         5
#define CYAN            6
#define WHITE           7

void textcolor(int attr, int fg, int bg);
int main() {
        textcolor(BRIGHT, RED, BLACK);
        printf("In color\n");
        textcolor(RESET, WHITE, BLACK);
        return 0;
}

void textcolor(int attr, int fg, int bg) {
        char command[13];

        /* Command is the control command to the terminal */
        sprintf(command, "%c[%d;%d;%dm", 0x1B, attr, fg + 30, bg + 40);
        printf("%s", command);
}


Was This Post Helpful? 0
  • +
  • -

#5 paakfaat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 04-August 08

Re: color text in C programming

Posted 04 August 2008 - 09:19 PM

Thanks for your reply
I tried it in DEV-C++ and compiled it.
But the output is
[1;31;40mIn color
[0;37;40m
and something that the keyboard don't have.
The color still didn't change at all.
So what can I do?

This post has been edited by paakfaat: 04 August 2008 - 09:23 PM

Was This Post Helpful? 0
  • +
  • -

#6 Guest_Whizzy*


Reputation:

Re: color text in C programming

Posted 04 August 2008 - 09:28 PM

I hate to steer anyone away from DIC, however since your question is compiler specific, have you tried to go to the forums on Bloodshed.com?
Was This Post Helpful? 0

#7 paakfaat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 04-August 08

Re: color text in C programming

Posted 04 August 2008 - 09:38 PM

Bloodshed.com? I didn't heard it before. Let's go and see. Thank you. But any guideline? Or I will check it out myself.
Also what is DIC? I guess it is another C compiler. Where can I download it

This post has been edited by paakfaat: 04 August 2008 - 09:49 PM

Was This Post Helpful? 1
  • +
  • -

#8 gabehabe  Icon User is offline

  • GabehabeSwamp
  • member icon




Reputation: 1382
  • View blog
  • Posts: 10,962
  • Joined: 06-February 08

Re: color text in C programming

Posted 05 August 2008 - 02:33 AM

This is DIC! Dream In Code!

And Bloodshed are the makers of Dev-C++ you should see "Bloodshed Dev-C++" on the splash screen :)
Was This Post Helpful? 1
  • +
  • -

#9 paakfaat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 04-August 08

Re: color text in C programming

Posted 05 August 2008 - 04:39 AM

Ok. thank you very much.
so is the differences between DIC and Dev C++ ?
So is DIC also a compiler?
Is DIC also a C porgramming language?
I would like to know where can I download it.

This post has been edited by paakfaat: 05 August 2008 - 05:07 AM

Was This Post Helpful? 0
  • +
  • -

#10 Hyper_Eye  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 39
  • View blog
  • Posts: 116
  • Joined: 13-September 07

Re: color text in C programming

Posted 05 August 2008 - 06:10 AM

View Postpaakfaat, on 5 Aug, 2008 - 06:39 AM, said:

Ok. thank you very much.
so is the differences between DIC and Dev C++ ?
So is DIC also a compiler?
Is DIC also a C porgramming language?
I would like to know where can I download it.


DIC is this entire website. It is not a compiler. DIC is Dream In Code which is the website you are posting on here.
Was This Post Helpful? 0
  • +
  • -

#11 paakfaat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 04-August 08

Re: color text in C programming

Posted 05 August 2008 - 06:40 AM

ok, thanks.
But how can I write the text with more than 15 color. Since Whizzy said that the problem is compiler specific. I asked the probelm in the forums of Bloodshed.com but there are no reply. Therefore, any C compiler can work with more than 15 colors. Or any other method can help me to write text with more than 15 colors?

As no2pencil gives a suggestion. What compiler does he use? So that his program can be compiled successfully?

This post has been edited by paakfaat: 05 August 2008 - 06:42 AM

Was This Post Helpful? 0
  • +
  • -

#12 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: color text in C programming

Posted 05 August 2008 - 07:28 AM

Setting the text color is not AFAIK part of Standard C.

There are however a few results here.

Next time please use Google first. :)
Was This Post Helpful? 0
  • +
  • -

#13 paakfaat  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 20
  • Joined: 04-August 08

Re: color text in C programming

Posted 05 August 2008 - 07:39 AM

I did use google. And I searched for 3 days. But I'm new to programming. My knowledge is very limited. So I'm sorry about it. Thank you anyway. I will check again.
But what is gcc? I'm using window. But most of the results is about linux and unix.

This post has been edited by paakfaat: 05 August 2008 - 07:42 AM

Was This Post Helpful? 0
  • +
  • -

#14 Tom9729  Icon User is offline

  • Segmentation fault
  • member icon

Reputation: 180
  • View blog
  • Posts: 2,641
  • Joined: 30-December 07

Re: color text in C programming

Posted 05 August 2008 - 07:41 AM

View Postpaakfaat, on 5 Aug, 2008 - 10:39 AM, said:

I did use google. And I searched for 3 days. But I'm new to programming. My knowledge is very limited. So I'm sorry about it. Thank you anyway. I will check again.

That's no problem. I only mention it because a lot of people post questions here that could easily be answered by doing a Google search, but they're too lazy.

In the future you should probably mention that you've tried searching, so that grumpy people like me leave you alone. :)
Was This Post Helpful? 0
  • +
  • -

#15 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: color text in C programming

Posted 05 August 2008 - 08:08 AM

Color is not really a "compiler" specific problem but a platform specific problem. What platform are you on (windows/linux).

Generally in Linux you can use escape sequences, while in windows you generally need to use the windows API.

There IS a port for the Borland conio.h for Linux -- which is one of the best cross platform solutions to the problem.

This post has been edited by NickDMax: 05 August 2008 - 08:09 AM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3