
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:
- 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.
- 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 keyboardint 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:
Number | Color | Number | Color |
---|---|---|---|
0 | Black | 8 | Bright Black |
1 | Blue | 9 | Bright Blue |
2 | Green | 10 | Bright Green |
3 | Cyan | 11 | Bright Cyan |
4 | Red | 12 | Bright Red |
5 | Magenta | 13 | Bright Magenta |
6 | Brown | 14 | Yellow |
7 | White | 15 | Bright 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.

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:

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:

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.