Hello, world! — ncurses

This program implements the good old "Hello, world!" application using ncurses. It clears the screen, displays the phrase in the centre, waits for a few seconds, then exits.

Programming Issues

All programs using ncurses must call initscr() before using any of the library functions. This function can fail, so we always check the return value.

We output the "Hello, world!" phrase using mvaddstr, or "move to the specified position and add a string there". Note that the coordinates are specified in the order of row, column, or y, x, and not the other way around.

Note that the above function (along with most other library functions) do not actually directly modify the display; they just modify data structures held in memory. To actually reflect our changes on the screen, we must call one of the refresh() functions.

We call the sleep() function to sleep for a few seconds, to show the full screen display for a moment before our command prompt returns. Note that sleep() here is a UNIX function and defined in <unistd.h>; if you are using a version of ncurses ported to a non-UNIX platform, your compiler may not have this header file or this function, though it will almost certainly have something similar.

Finally, we must clean up after ourselves, as we want the terminal to be in a useable state after our program exits. First, we must delete all of our windows using delwin(), including the one returned by initscr(). Then, we call endwin() and finally refresh() again, before we exit safely.

Note that when you are learning ncurses development, you will almost certainly foul up your terminal with alarming regularity. On UNIX, you can type stty sane at the command prompt to return the terminal to a useable state without needing to log out.


You will need to link to the ncurses library in order to use the functions in it. On UNIX, this is typically done using the -l compiler switch, e.g.:

[paul@localhost src]$ cc -o curhello curhello.c -lncurses

On other systems, consult your compiler's documentation, or the ncurses documentation for more information.

Also note that in most of these ncurses examples, we assume a terminal with 80 columns and 25 rows. If your terminal has different dimensions, you may need to adapt the examples to suit.

Source and Downloads