DEV Community

Cover image for Introduction to Ncurses (Part 1)
Gurkirat Singh
Gurkirat Singh

Posted on • Updated on

Introduction to Ncurses (Part 1)

Welcome to the series of Ncurses library in C++. In this, you will learn basics and some tidbits of ncurses.

NCurses is a programming library that provides an application programming interface that allows the programmer to write text-based user interfaces in a terminal independent manner.

To use ncurses we use ncurses.h header

#include <ncurses.h>
Enter fullscreen mode Exit fullscreen mode

Some functions that must be used in all ncurses

  • initscr()
    • initializes screen
    • sets up memory and clean the screen
  • refresh()
    • refreshes the screen to match what's in the memory on the screen
  • endwin()
    • deallocates memory and ends ncurses
  • getch()
    • waits for user input
    • returns the ASCII value of that key

To compile we must link the libncurses while compilation.

$ g++ <program file> -lncurses -o <output file name>
Enter fullscreen mode Exit fullscreen mode

An obligatory hello-world program in ncurses

#include <ncurses.h>
using namespace std;

int main(int argc, char ** argv)
{
    // init screen and sets up screen
    initscr();

    // print to screen
    printw("Hello World");

    // refreshes the screen
    refresh();

    // pause the screen output
    getch();

    // deallocates memory and ends ncurses
    endwin();
    return 0;
}
Enter fullscreen mode Exit fullscreen mode

Before using ncurses make sure it's installed in your system
Programs using NCurses

Here are some tools and IDE using ncurses library

  • HTop
  • GNU Nano Text Editor

Top comments (0)