SergeantPenguin SergeantPenguin - 16 days ago 9
C Question

Displaying accented characters with pdcurses

#include <curses.h>

int main(){

initscr();

for(int i = -128; i < 128; ++i)
addch(i);

getch();
endwin();

}


enter image description here

Pdcurses is displaying blanks for characters -128 to 0 (128 to 255). Is there any way to get, at least, the accented characters such as é to display properly?
iostream
has no trouble:

int main(){

for(int i = -128; i < 128; ++i)
std::cout << char(i);

}


enter image description here

I compiled pdcurses with wide character and UTF support, although that's surely not the problem here anyway, right? (Since the characters I want are contained within 0-255, and using
add_wch
instead didn't solve the issue anyway).

Windows 10 64-bit with g++ 6.1.0.

Answer

I'm very silly. addch takes a value of type chtype. The documentation isn't too clear on the actual type of chtype other than it represents characters. Looking at curses.h I can see that chtype is of type unsigned long. So the negative i values were being cast to unsigned and hence wrap around, causing curses to have to print characters for which their is no assigned ASCII representation. I was also getting lost because this seemingly similar code using no negative values produced the same result:

int main(){

    initscr();

    for(int i = 0; i < 255; ++i)
        addch(char(i));

    getch();
    endwin();

}

Until I remembered that char by default might be signed, and so char(i) of course wraps around to become negative before addch is called.