4

I've been looking at using colours in a DOS program I'm writing in C. I was told that conio.h has the textcolor() function, but when I use it in my code, the compiler/linker throws errors at me saying I've an undefined reference to the function.

Does conio.h actually have this function or have I been told bull?

starball
  • 20,030
  • 7
  • 43
  • 238
phillid
  • 198
  • 1
  • 11
  • 5
    That was a Borland function, never part of the C standard and written before prefixing non-standard functions with an underscore became the law. You'll have to break into a museum or use SetConsoleTextAttribute(). – Hans Passant Dec 30 '11 at 01:04
  • Ok, thanks. Is SetConsoleTextAttribute() supported by most 16-bit C compilers for DOS? :) – phillid Dec 30 '11 at 01:37
  • 1
    It's supported by none of them. Head for the museum or excavate Ralph's interrupt list. – Hans Passant Dec 30 '11 at 01:39
  • But you said to use SetConsoleTextAttribute? – phillid Dec 30 '11 at 01:40
  • 2
    Yes, my mistake, I haven't written any 16-bit code for the past 17 years. I've also haven't played any LPs for that long, forgot what OJ Simpson did and the silly putty turned into rock. You'll have to forgive an old man getting feeble. – Hans Passant Dec 30 '11 at 01:49
  • Haha ok, I will give that function a go anyway – phillid Dec 30 '11 at 01:50
  • SetConsoleTextAttribute is a Win32 console function. For DOS, DJGPP's library has a textcolor(), but DJGPP needs a 386. For 16-bit, Watcom has some functions that are similar. – Greg Inozemtsev Dec 30 '11 at 01:59
  • I'm using Watcom to compile, I will have to find out those functions... – phillid Dec 30 '11 at 02:01
  • I'm looking at Section 2.3.5 in the [OpenWatcom manual](http://www.openwatcom.org/ftp/manuals/current/clib.pdf) – Greg Inozemtsev Dec 30 '11 at 02:24
  • this will light my day... haven't heard of Ralph's INT list for ages. Sir you'll make my day a better one (http://www-2.cs.cmu.edu/afs/cs/user/ralf/pub/WWW/files.html) – BigMike Dec 30 '11 at 08:28
  • never heard of Ralf's init list (I suppose I am a little newer to this world than you guys) so I checked it, download and extracted the archive**s**. OH MY GOD :)) I feel like this guy said: `Hey, internet isn't yet a never ending repository of knowledge, references, examples, how to's and stack overflows (wink wink), so I'll just create it... by my self... in one place... ok... sounds duable, let's do it.` And we kids today complain with all those abundantly available at our fingers. – bolov May 28 '15 at 18:55

3 Answers3

2

No, the conio.h library does not have the textcolor function defined. One of the ways that that function could be defined is the following (include the windows.h library):

void textcolor (int color)
{
    static int __BACKGROUND;

    HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );
    CONSOLE_SCREEN_BUFFER_INFO csbiInfo;


    GetConsoleScreenBufferInfo(h, &csbiInfo);

    SetConsoleTextAttribute (GetStdHandle (STD_OUTPUT_HANDLE),
                             color + (__BACKGROUND << 4));
}
Alejandro Caro
  • 998
  • 1
  • 10
  • 22
0

It's a Turbo C/C++ Compiler function. It is in the conio.h header, but only if you're using a Turbo C/C++ Compiler.

See page 384 of the Turbo C/C++ Compiler 2.0 docs, which says:

Function Selects new character color in text mode.

Syntax

#include <conio.h>
void textcolor(int newcolor); 

Prototype in conio.h

Remarks: textcolor selects the foreground character color. The foreground color of all characters subsequently written by the console output functions will be the color given by newcolor. You can give the color using a symbolic constant defined in conio.h. If you use these constants, you must include conio.h.

This function does not affect any characters currently on the screen, but only those displayed using direct console output (such as cprintf) after textcolor has been called.

[...]

See also: Colorizing text in Turbo C++.

starball
  • 20,030
  • 7
  • 43
  • 238
-1

Check the textcolor library library, it may do just what you need.

An example, showing how to use it:

#include<stdio.h>
#include<conio.h>
main()
{
   textcolor(RED);
   cprintf("C programming");

   getch();
   return 0;
}
Morten Jensen
  • 5,818
  • 3
  • 43
  • 55
Rahul
  • 41
  • 1
  • 10
  • "check", there is the code you wrote, but this code does not work, because textcolor is not included in conio.h – serge Nov 30 '20 at 13:23