{"id":12034,"date":"2025-10-10T02:00:00","date_gmt":"2025-10-10T06:00:00","guid":{"rendered":"https:\/\/www.both.org\/?p=12034"},"modified":"2025-09-26T14:50:01","modified_gmt":"2025-09-26T18:50:01","slug":"print-hello-world-in-color-with-conio","status":"publish","type":"post","link":"https:\/\/www.both.org\/?p=12034","title":{"rendered":"Print &#8216;Hello world&#8217; in color with conio"},"content":{"rendered":"<div class=\"pld-like-dislike-wrap pld-template-1\">\r\n    <div class=\"pld-like-wrap  pld-common-wrap\">\r\n    <a href=\"javascript:void(0)\" class=\"pld-like-trigger pld-like-dislike-trigger  \" title=\"\" data-post-id=\"12034\" data-trigger-type=\"like\" data-restriction=\"cookie\" data-already-liked=\"0\">\r\n                        <i class=\"fas fa-thumbs-up\"><\/i>\r\n                <\/a>\r\n    <span class=\"pld-like-count-wrap pld-count-wrap\">1    <\/span>\r\n<\/div><\/div>\n<p>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.<\/p>\n\n\n\n<p>So it&#8217;s probably not surprising that FreeDOS includes a C compiler\u2014along with other programming languages. The FreeDOS distribution includes several C compilers you can use to learn C programming, including Bruce&#8217;s C compiler (a simple C compiler), the IA-16 port of GCC (requires a &#8216;386 or better CPU to compile, but the generated programs can run on low-end systems), and the OpenWatcom C compiler.<\/p>\n\n\n\n<p>Programming in C on FreeDOS is basically the same as C programming on Linux, with two exceptions:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>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.<\/li>\n\n\n\n<li>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.<\/li>\n<\/ol>\n\n\n\n<p>The OpenWatcom C compiler is a great compiler for first-time programmers, so let&#8217;s start there. If you don&#8217;t have this installed by default, you can install it from the BonusCD in the FreeDOS 1.4 distribution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"programming-on-dos\">Programming on DOS<\/h2>\n\n\n\n<p>To briefly describe a few of the most useful functions available to you in the Open Watcom C compiler, use these functions from <code>conio.h<\/code> to read data directly from the keyboard:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>int getch(void)<\/code> will get a single keystroke from the keyboard<\/li>\n\n\n\n<li><code>int getche(void)<\/code> will do the same, but also echo it<\/li>\n<\/ul>\n\n\n\n<p>And these functions from <code>graph.h<\/code> will help you to print data directly to the screen:<\/p>\n\n\n\n<p><code>_settextcolor(short color)<\/code> will set the text color, and <code>_setbkcolor(short color)<\/code> will set the text background color. Use <code>_settextposition(short y, short x)<\/code> to move the print position to row <em>y<\/em> and column <em>x<\/em>, and use <code>_outtext(char _FAR *string)<\/code> to print to the screen at that location.<\/p>\n\n\n\n<p>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:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>Number<\/th><th>Color<\/th><th>Number<\/th><th>Color<\/th><\/tr><\/thead><tbody><tr><td>0<\/td><td>Black<\/td><td>8<\/td><td>Bright Black<\/td><\/tr><tr><td>1<\/td><td>Blue<\/td><td>9<\/td><td>Bright Blue<\/td><\/tr><tr><td>2<\/td><td>Green<\/td><td>10<\/td><td>Bright Green<\/td><\/tr><tr><td>3<\/td><td>Cyan<\/td><td>11<\/td><td>Bright Cyan<\/td><\/tr><tr><td>4<\/td><td>Red<\/td><td>12<\/td><td>Bright Red<\/td><\/tr><tr><td>5<\/td><td>Magenta<\/td><td>13<\/td><td>Bright Magenta<\/td><\/tr><tr><td>6<\/td><td>Brown<\/td><td>14<\/td><td>Yellow<\/td><\/tr><tr><td>7<\/td><td>White<\/td><td>15<\/td><td>Bright White<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"hello-world\">Hello world<\/h2>\n\n\n\n<p>The first program many new developers learn to write is a program that just prints &#8220;Hello world&#8221; to the user. We can use the DOS &#8220;conio&#8221; and &#8220;graphics&#8221; libraries to make this a more interesting program and print &#8220;Hello world&#8221; in a rainbow of colors.<\/p>\n\n\n\n<p>In this case, we&#8217;ll iterate through each of the text colors, from 0 (Black) to 15 (Bright White). As we print each line, we&#8217;ll indent the next line by one space. When we&#8217;re done, we&#8217;ll wait for the user to press any key, then we&#8217;ll reset the screen and exit.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;stdio.h&gt;\n#include &lt;conio.h&gt;\n#include &lt;graph.h&gt;\n\nint\nmain()\n{\n    short color;\n    short row = 1, col = 1;\n\n    _setvideomode(_TEXTC80);\n\n    for (color = 0; color &lt;= 15; color++) {\n        _settextposition(row++, col++);\n        _settextcolor(color);\n        _setbkcolor(color ? 0 : 7); <em>\/* white background for color=0 *\/<\/em>\n        _outtext(\"Hello world\");\n    }\n\n    getch();\n    _setvideomode(_DEFAULTMODE);\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<p>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.<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"533\" height=\"400\" src=\"http:\/\/www.both.org\/wp-content\/uploads\/2025\/09\/image1-1.png\" alt=\"source code to the 'hello world' program in the FED editor, with blue background and white text\" class=\"wp-image-12031\"\/><\/figure>\n\n\n\n<p>Before you can compile using OpenWatcom, you&#8217;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 <code>C:\\devel\\watcomc\\owsetenv.bat<\/code>, which will automatically set up your environment for OpenWatcom.<\/p>\n\n\n\n<p>After your environment is ready to go, you can use the OpenWatcom compiler to compile this &#8220;Hello world&#8221; program. I&#8217;ve saved my C source file as <code>test.c<\/code> so I can use <code>wcl test.c<\/code> to compile and link the program into a DOS executable, called <code>test.exe<\/code>. By default, the OpenWatcom compiler prints a lot of output as it runs the Watcom C Compiler (<code>wcc<\/code>) and the Watcom linker (<code>wlink<\/code>) to generate the program:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"533\" height=\"400\" src=\"http:\/\/www.both.org\/wp-content\/uploads\/2025\/09\/image2-1.png\" alt=\"output while compiling the test.c program, with a lot of extra text\" class=\"wp-image-12032\"\/><\/figure>\n\n\n\n<p>If you don&#8217;t see any error messages when compiling the C source file, you can now run your DOS program. Run <code>test.exe<\/code> on the DOS command line to run the new program, and you should see this very pretty output:<\/p>\n\n\n\n<figure class=\"wp-block-image aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"533\" height=\"400\" src=\"http:\/\/www.both.org\/wp-content\/uploads\/2025\/09\/image3-1.png\" alt=\"the text 'hello world' printed in different colors in a step pattern from upper left to lower right\" class=\"wp-image-12033\"\/><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"get-started-and-learn-more\">Get started and learn more<\/h2>\n\n\n\n<p>C is a very efficient programming language that works well for writing programs on limited-resource systems like DOS. There&#8217;s lots more that you can do by programming in C on DOS. If you&#8217;re new to the C language, you can learn C yourself by following along in this <a href=\"https:\/\/www.youtube.com\/watch?v=6fThPftZuAQ&amp;list=PLzuACU-W7Omo3VEnMKuM0IPupdOHFDzL3&amp;index=67\">how-to video series<\/a> on the <a href=\"https:\/\/www.youtube.com\/freedosproject\">FreeDOS YouTube channel<\/a>.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p><em>This article is adapted from <a href=\"https:\/\/opensource.com\/article\/21\/6\/program-c-freedos\">How to program in C on FreeDOS<\/a> by Jim Hall, and is republished with the author&#8217;s permission.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Learn how to write your first C program on FreeDOS with this example.<\/p>\n","protected":false},"author":33,"featured_media":12033,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","footnotes":""},"categories":[340,150],"tags":[267,152],"class_list":["post-12034","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-freedos","category-programming","tag-freedos","tag-programming"],"modified_by":"Jim Hall","_links":{"self":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/12034","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/users\/33"}],"replies":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=12034"}],"version-history":[{"count":4,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/12034\/revisions"}],"predecessor-version":[{"id":12038,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/posts\/12034\/revisions\/12038"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=\/wp\/v2\/media\/12033"}],"wp:attachment":[{"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=12034"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=12034"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.both.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=12034"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}