Whole document tree
    

Whole document tree

About output functions like printw()

6. About output functions like printw()

I guess you can't wait any more to see some action. Back to our odyssey of curses functions. Now that curses is initialized, let's interact with world.

There are three classes of functions which you can use to do output on screen.

  1. addch() class: Print single character with attributes

  2. printw() class: Print formatted output similar to printf()

  3. addstr() class: Print strings

These functions can be used interchangeably and it's a matter of style as to which class is used. Let's see each one in detail.

6.1. addch() class of functions

These functions put a single character into the current cursor location and advance the position of the cursor. You can give the character to be printed but they usually are used to print a character with some attributes. Attributes are explained in detail in later sections of the document. If a character is associated with an attribute(bold, reverse video etc.), when curses prints the character, it is printed in that attribute.

In order to combine a character with some attributes, you have two options:

  • By OR'ing a single character with the desired attribute macros. These attribute macros could be found in the header file ncurses.h. For example, you want to print a character ch(of type char) bold and underlined, you would call addch() as below.
        addch(ch | A_BOLD | A_UNDERLINE);

  • By using functions like attrset(),attron(),attroff(). These functions are explained in the Attributes section. Briefly, they manipulate the current attributes of the given window. Once set, the character printed in the window are associated with the attributes until it is turned off.

Additionally, curses provides some special characters for character-based graphics. You can draw tables, horizontal or vertical lines, etc. You can find all avaliable characters in the header file ncurses.h. Try looking for macros beginning with ACS_ in this file.

6.3. printw() class of functions

These functions are similar to printf() with the added capability of printing at any position on the screen.