4

I just started learning programming in C. the first problem was to choose on which platform should I learn it, and I selected Ubuntu. I found a GCC compiler to compile my projects, and it worked fine for me. I was running my compiled projects through Terminal. But when I wanted to write a program which have to show a text on a colorful background, I understood that Terminal is not helping me. Actually I am learning from lessons written for programming on Windows, and they use there Borland C++ 5.2, but these lessons were written in 2007

So I am wondering, where I should run my compiled program in Ubuntu 10.10, to see the result properly? Actually I tried to transfer to Windows, but Ubuntu on my computer works much faster, and it is easier I think to work in Ubuntu rather than Windows.

Thomas Dickey
  • 51,086
  • 7
  • 70
  • 105
454b
  • 133
  • 2
  • 6
  • 12

3 Answers3

7

The simplest way to print color message without any toolkit is to use VT100 escape commands: http://www.termsys.demon.co.uk/vtansi.htm#colors

So you can write something like this:

printf("\x1b[31mThis is red text\x1b[0m\n");
printf("\x1b[32mThis is green text\x1b[0m\n");

But this code works only for VT100-compatible terminals (almost all terminals of the Unix-system). The best way is to check TERM environment variable before colorizing your output.

Disclaimer: if you want to write code capable with all terminal types then you should use ncurses-like library.

Dmitry Poroh
  • 3,705
  • 20
  • 34
3

The ncurses library does exactly what you are looking for. Using this library, you can choose to write any text, anywhere on the screen with any text/background color.

You can read more on it on Wikipedia. You can install it under ubuntu with:

$ sudo apt-get install libncurses5-dev
Shahbaz
  • 46,337
  • 19
  • 116
  • 182
0

If you want to start with GUI programming in Linux, you will probably have to look into one of GUI toolkits, which is a library to write GUI elements, widgets, etc.

One of these toolkints is Gtk, which you can start to learn via Gtk tutorial. Another option is Qt which is probably easier to use, but that is based on C++.

I'm afraid that Borland C++ Windows courses are pretty much unusable for Linux GUI programming.

Or, if you just to want a colored terminal text, look into this question: stdlib and colored output in C

Community
  • 1
  • 1
che
  • 12,097
  • 7
  • 42
  • 71
  • thanks for answering. but actually I just want to run a project written in C, which contains colorful text. like in borland c++, after compiling your prject, when you run it, it is opened in another window, with black background, and if your program contains code that will change this background color, it will. but when you do this in terminal on ubuntu, you won`t get this result – 454b Jul 26 '12 at 15:48
  • @EmiLazE: Oh, changing background colors is possible. Check out the link I added. – che Jul 26 '12 at 15:52
  • you know, I want to do this on Ubuntu : http://ipg.h1.ru/lessons/ci/les18.files/2.gif – 454b Jul 26 '12 at 15:53
  • ok, thanks, as soonn as I will check it I will inform you, hope that it will work for me)) just one more question: this will change the text color in terminal, right? – 454b Jul 26 '12 at 15:54
  • 1
    @EdwinBuck, his sentence "when I wanted to write a program which have to show a text on a colorful background" clearly indicates that he wants his output to be colorful, not his IDE. – Shahbaz Jul 26 '12 at 15:57
  • @che I checked your link, yes it works, but I have a question what is incorrect here: #include #include main() { textcolor("magenta"); printf("Hello!\n"); } – 454b Jul 26 '12 at 16:26
  • 1
    Do note that your "project written in C" is *not* using any standard C libraries to output colored text! It is using Borland APIs that simply don't exist on Linux. You're free to implement them yourself using ncurses if you so wish, of course. Myself, I'd rather leverage ncurses and accept that old Borland C console code is not portable and won't work elsewhere. – Kuba hasn't forgotten Monica Jul 26 '12 at 16:43
  • @KubaOber, I understood already that borland C codes are not working elsewhere. so now, when I learned how to change text color, is it possible to change background color using C? – 454b Jul 26 '12 at 16:48
  • Don't ask about "C", since you're implicitly asking about C standard libraries and those don't provide any support for that. You can hand-code some ANSI color sequences. Or you can use ncurses. Which is it that you are asking about? – Kuba hasn't forgotten Monica Jul 26 '12 at 20:44
  • @KubaOber, I`d be very happy if you help me find values of colors for ncurses like red is \E[31m, green is \E[32m (I tried to write "x1b" instead of "E", and it worked. why?) – 454b Jul 27 '12 at 14:04