Thursday, November 29, 2007

Examples

Hello, world! Consider the following Turbo C program:
#include
#include

void
main (void)
{
time_t t, newt;
int i = 0, x, y;
time (&t);
clrscr ();
cprintf ("Hit any key to continue: ");
x = wherex ();
y = wherey ();
// Wait for a keypress, flashing the message
// "I'm waiting!" on and off once per second.
// After any key is hit, display "Hello, world!"
while (!kbhit ())
{
time (&newt);
if (t != newt)
{
t = newt;
i++;
if (0 == (i & 1))
cprintf ("I\'m waiting!");
clreol ();
gotoxy (x, y);
}
}
getch ();
cprintf ("\r\nHello, world!\r\n");
}

This program almost compiles with GNU gcc (and the TurboC library), and almost works as-is, except for a couple of points:
  • You will want to add the following statement at the very end of the program:
    • textmode (EXITMODE);
  • The "Hello, world!" message won't actually be seen, because the you have to end with will instantly restore whatever was on the screen prior to running the program. So, you might want to add another getch() textmode(EXITMODE) printing the "Hello, world!" message.
  • GNU gcc expects main to be of type int rather than void, and of course you can't actually use int because of the automatic conversion of integer datatypes. So use gint instead.
Having done these things, you'll find that you can compile the program and run it without any errors:
gcc -o Hello Hello.c -lTurboC -lncurses
xterm +sb -e Hello
Compiling without warnings is actually a little unusual, because there are quite a few things that can cause non-fatal warning messages. See the trouble-shooting section for more detail.

No comments: