<curses.h> (typically <ncurses.h>) provides terminal control for building text-based user interfaces: window creation, cursor positioning, text attributes, and input handling. It translates portable abstractions into the right escape sequences for any terminal.
Use it to build full-screen TUI applications that work across different terminal types.
This example initializes ncurses, draws text at a specific position, and waits for user input.
// compile: gcc -o cursesexample cursesexample.c -lncurses // run: ./cursesexample // description: draw positioned text and handle input #include <ncurses.h> #include <unistd.h> int main() { initscr(); int h, w; getmaxyx(stdscr, h, w); mvprintw(h/2, w/2 - 8, "Hello, ncurses!"); mvprintw(h/2 + 1, w/2 - 10, "Terminal: %d x %d", w, h); refresh(); sleep(2); endwin(); return 0; }
Initialization and cleanup:
initscr(): initialize ncurses, return main windowendwin(): clean up and restore terminalrefresh(): update screen displayclear(): clear screencbreak(): disable line bufferingnoecho(): disable echo of user inputWindow management:
newwin(h, w, y, x): create new windowdelwin(w): delete windowmvwin(w, y, x): move windowgetmaxyx(w, &y, &x): query window sizestdscr: standard screen (main window)Cursor and output:
move(y, x): move cursormvprintw(y, x, fmt, ...): print at positionmvaddstr(y, x, str): add string at positionaddch(c): add single characterattron(attr), attroff(attr): enable/disable text attributesA_BOLD, A_DIM, A_REVERSE, A_STANDOUT: text attributesInput:
getch(): get character from inputgetstr(s): get string inputscanw(fmt, ...): formatted inputColors (if supported):
has_colors(): check if terminal supports colorsstart_color(): enable color modeinit_pair(id, fg, bg): define color pairCOLOR_RED, COLOR_GREEN, COLOR_BLUE, COLOR_WHITE, etc.: color constants