Does C depend on ASCII?

Doug Gwyn gwyn at brl-vgr.ARPA
Sat May 12 14:28:17 AEST 1984


Traditionally C has used the host computer "native" character set
(how can a convention be "native"? you ask; yet it really is).
However many programs written in C implicitly assume that the
character set is ASCII, although the language doesn't guarantee this.

I seem to recall that the C Language Standards Committee addressed
this question but I don't remember whether they decided that ASCII is
the "official" C character set.

For my own use in those few cases where the character codes are
important, I have the following lines in my standard header file:
/* integer (or character) arguments and value: */
/* THESE PARTICULAR DEFINITIONS ARE FOR ASCII HOSTS ONLY */
#define tohostc( c )	(c)		/* map ASCII to host char set */
#define tonumber( c )	((c) - '0')	/* convt digit char to number */
#define todigit( n )	((n) + '0')	/* convt digit number to char */

The idea is to use toascii() to map the native input characters to
internal ASCII form, although you then have to do the same to the
C character constants against which the mapped input characters are
to be compared (or else use numerical ASCII codes).  Then on output
one uses tohostc() to map the internal form back to native chars.
Obviously there is non-negligible run-time overhead if the host
character set is not ASCII but something stupid like EBCDIC, but I
am willing to live with this in order to not have to change my source
code when I port it to a non-ASCII machine (just the standard header
needs to be changed).



More information about the Comp.unix mailing list