How can I tell which way my terminal expects to received data? Should I use fputc
, fputwc
, or something else?
Here is what I've tried with fputc
:
// characters.c
#include <ctype.h>
#include <stdio.h>
int main ( void )
{
for ( int c = 0 ; c <= 65535 ; c ++ )
if ( isgraph ( c ) ) fputc ( c , stdout ) ;
}
And with fputwc
:
// wcharacters.c
#include <ctype.h>
#include <stdio.h>
#include <wcahr.h>
int main ( void )
{
for ( wchar_t c = 0 ; c <= 65535 ; c ++ )
if ( isgraph ( c ) ) fputwc ( c , stdout ) ;
}
With characters.c
, the output is consistent with the ASCII characters for the first few items printed on the terminal (!"#
, etc.), then, after ~
, it's garbage.
I get similar results with wcharacters.c
, except that instead of garbage it's ?
's or some other ASCII character (but with the great majority being ?
's).
I know the same terminal supports many character representations in in the Unicode code point range 33 to 65535 (decimal), as I can print many of those characters using Python 3.
I am using Trisquel GNU/Linux, gcc (-std=c99
), and the "MATE Terminal." The font is "Monospace," which sounds generic, but it seems to support many more characters beyond the ASCII range.
I am open to using a more recent C standard. At the same time, one of the points of the project I am working on is simplicity, and the C standard seems to become more and more complicated with each successive standard (would using C17's uchar.h
be simpler?).
With the function I am working on, the user should be able to write any of the graphic characters (as in isgraph
) to stdout
and have the appearance of those characters be "correct."
Addendum
In response to @chux-reinstate-monica:
isgraph()
OK with narrow characters, not for wide ones.
A better test than characters.c
might be:
// charactersAll.c
#include <ctype.h>
#include <limits.h>
#include <stdio.h>
int main ( void )
{
const int cmax = (
(unsigned) UCHAR_MAX < (unsigned) INT_MAX
) ? (int) UCHAR_MAX : INT_MAX ;
int c = -1 ;
while ( c < cmax )
{
c ++ ;
if ( isgraph ( c ) ) fputc ( c , stdout ) ;
}
}
vs. characters.c:
loop through full range of valid
isgraph
arguments.avoid overflow:
characters.c
potentially attempts to incrementc
past maximum value (i.e., whenint
is 16-bit; acknowledgement: @chux-reinstate-monica, "aside").