Ncurses makes C all warm and fuzzy

Software

CC from Code Geass

Get it? CC cuddling cheese-kun? Warm and fuzzy? C? Get it? When I first started messing around with GNU ncurses I was under the impression it was a simple framework to create interactive, windowed console applications…

In reality it's a complete, really polished environment that extends C in some really nice ways; so much so that I would argue it would be a misnomer to say you're simply writing "C" code any more. It's to C what Cream is to Vim. It's CC with Cheese Kun. I could go on.

Header files

The first sign you get that ncurses is something special is that importing it's single header file gives you all these below header files automatically.

#include <ncurses.h>
#include <stdio.h>
#include <stdarg.h>
#include <stddef.h>
#include <unctrl.h>

In fact you're actively encoraged not to import any of these usual suspects because you'd needlessly inflate your compiled applications. I assume it's doing some clever tricks to only use what's nessisary. It also adds a bunch of it's own functions that provide similar functionality, more on that below.

Booleans

Most people start with C and move on to C++, I went the other way… you can probably see where this is going! When you import <ncurses.h> you get a boolean data type for C for free. Paroozing the header file it seems to cleverly make sure it doesn't conflict with the C++ bool if that's the language you're using, pretty cool.

#undef TRUE
#define TRUE    1
#undef FALSE
#define FALSE   0
typedef unsigned char NCURSES_BOOL;
#if defined(__cplusplus) /* __cplusplus, etc. */
/* use the C++ compiler's bool type */
#define NCURSES_BOOL bool
#else /* c89, c99, etc. */
[…]

Alternatives to standard in/output

This is what I mean by ncurses almost being like an entire environment for C, it makes so much stuff easier and more fun. As well as supporting the basic C output functions such as printf(), ncurses comes with a bunch of extras that make sense when dealing with GUI-like interfaces in a terminal.

addch();  /* add character to the "window" */
addstr(); /* add string of characters */
printw(); /* equivilent to C's printf() */

What's also really cool though are the functions for reading in data from the keyboard which is much nicer than vanilla C:

getch()   /* get character */
getstr()  /* get string of characters */
getnstr() /* same as above, but measured */
scanw()   /* equivilent to scanf(); */

Why?

I'm surprised by the number of people asking me why, in 2010, I'm interested in writing console applications, because we all know console applications are passe and unsexy. I'm hoping in the coming weeks when I start writing code for a few projects (and am hopefully allowed to publish some of it) you'll see why. There's still a ton of life in this environment and I'm really excited to see what I can accomplish in it.

Author bio and support

Me!

Ruben Schade is a technical writer and infrastructure architect in Sydney, Australia who refers to himself in the third person. Hi!

The site is powered by Hugo, FreeBSD, and OpenZFS on OrionVM, everyone’s favourite bespoke cloud infrastructure provider.

If you found this post helpful or entertaining, you can shout me a coffee or send a comment. Thanks ☺️.