the text 'hello world' printed in different colors in a step pattern from upper left to lower right

Print ‘Hello world’ in color with conio

When I first started using DOS, I enjoyed writing games and other interesting programs using BASIC, which DOS included. Much later, I learned the C programming language. I immediately loved working in C! It was a straightforward programming language that gave me a ton of flexibility for writing useful programs. In fact, much of the FreeDOS core utilities are written in C and Assembly.

So it’s probably not surprising that FreeDOS includes a C compiler—along with other programming languages. The FreeDOS distribution includes several C compilers you can use to learn C programming, including Bruce’s C compiler (a simple C compiler), the IA-16 port of GCC (requires a ‘386 or better CPU to compile, but the generated programs can run on low-end systems), and the OpenWatcom C compiler.

Programming in C on FreeDOS is basically the same as C programming on Linux, with two exceptions:

  1. You need to remain aware of how much memory you use. Linux allows programs to use lots of memory, but FreeDOS is more limited. Thus, DOS programs used one of four memory models (large, medium, compact, and small) depending on how much memory they needed.
  2. You can directly access the console. On Linux, you can create text-mode mode programs that draw to the terminal screen using a library like ncurses. But DOS allows programs to access the console and video hardware. This provides a great deal of flexibility in writing more interesting programs.

The OpenWatcom C compiler is a great compiler for first-time programmers, so let’s start there. If you don’t have this installed by default, you can install it from the BonusCD in the FreeDOS 1.4 distribution.

Programming on DOS

To briefly describe a few of the most useful functions available to you in the Open Watcom C compiler, use these functions from conio.h to read data directly from the keyboard:

  • int getch(void) will get a single keystroke from the keyboard
  • int getche(void) will do the same, but also echo it

And these functions from graph.h will help you to print data directly to the screen:

_settextcolor(short color) will set the text color, and _setbkcolor(short color) will set the text background color. Use _settextposition(short y, short x) to move the print position to row y and column x, and use _outtext(char _FAR *string) to print to the screen at that location.

DOS only supports sixteen text colors and eight background colors. You can use the values 0 (Black) to 15 (Bright White) to specify the text colors, and 0 (Black) to 7 (White) for the background colors:

NumberColorNumberColor
0Black8Bright Black
1Blue9Bright Blue
2Green10Bright Green
3Cyan11Bright Cyan
4Red12Bright Red
5Magenta13Bright Magenta
6Brown14Yellow
7White15Bright White

Hello world

The first program many new developers learn to write is a program that just prints “Hello world” to the user. We can use the DOS “conio” and “graphics” libraries to make this a more interesting program and print “Hello world” in a rainbow of colors.

In this case, we’ll iterate through each of the text colors, from 0 (Black) to 15 (Bright White). As we print each line, we’ll indent the next line by one space. When we’re done, we’ll wait for the user to press any key, then we’ll reset the screen and exit.

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

int
main()
{
    short color;
    short row = 1, col = 1;

    _setvideomode(_TEXTC80);

    for (color = 0; color <= 15; color++) {
        _settextposition(row++, col++);
        _settextcolor(color);
        _setbkcolor(color ? 0 : 7); /* white background for color=0 */
        _outtext("Hello world");
    }

    getch();
    _setvideomode(_DEFAULTMODE);

    return 0;
}

You can use any text editor to write your C source code. I like using the FED editor because it provides syntax highlighting, making it easier to see keywords, strings, and variables in my program source code.

source code to the 'hello world' program in the FED editor, with blue background and white text

Before you can compile using OpenWatcom, you’ll need to set up the DOS environment variables so OpenWatcom can find its support files. The OpenWatcom C compiler package includes a setup batch file that does this for you, as C:\devel\watcomc\owsetenv.bat, which will automatically set up your environment for OpenWatcom.

After your environment is ready to go, you can use the OpenWatcom compiler to compile this “Hello world” program. I’ve saved my C source file as test.c so I can use wcl test.c to compile and link the program into a DOS executable, called test.exe. By default, the OpenWatcom compiler prints a lot of output as it runs the Watcom C Compiler (wcc) and the Watcom linker (wlink) to generate the program:

output while compiling the test.c program, with a lot of extra text

If you don’t see any error messages when compiling the C source file, you can now run your DOS program. Run test.exe on the DOS command line to run the new program, and you should see this very pretty output:

the text 'hello world' printed in different colors in a step pattern from upper left to lower right

Get started and learn more

C is a very efficient programming language that works well for writing programs on limited-resource systems like DOS. There’s lots more that you can do by programming in C on DOS. If you’re new to the C language, you can learn C yourself by following along in this how-to video series on the FreeDOS YouTube channel.


This article is adapted from How to program in C on FreeDOS by Jim Hall, and is republished with the author’s permission.

Leave a Reply