First of all, char in C is a subtype of int,
which means that you can do normal arithmetic operations to chars
and that chars are allowed in int expressions without special action
needed.
for example:
char c;
c = 'A' + 1; /* c will be 'B' */
c = c - 'A' + 'a'; /* c will now be 'b' - lower case */
Compare this to Pascal for example, where char and integer are two
different and
incompatible types. To do the same as above in Pascal, you need
conversion functions
(which are part of the Pascal standard):
var c: char;
c := chr (ord ('A') + 1);
c := chr (ord (c) - ord ('A') + ord ('a'));
Back to C:
Now, because chars are small ints in C (much like shorts), they can of
course be signed or unsigned.
If we have 8 bit bytes (which is normal on today's machines, but C would
of course support other
architectures, too), the ranges are -128 .. 127 for signed char (2's
complement) and 0 .. 255 for unsigned char.
If you code
char c;
like in the example above, the char is always signed or unsigned
depending on the default
active during compilation (can probably be modified by compiler option;
if not, there will
be a "factory" setting).
This "factory" setting is unsigned for IBMs z/OS compilers, and signed
for most Windows
and Unix compilers I am aware of.
The reason may be the EBCDIC character set; if signed were used with
EBCDIC, all the
normal characters and digits (X'C1' = 'A', X'F0' = '0') would have
negative char values.
So in my opinion the choice of the unsigned char default is natural for
EBCDIC machines.
The difference between this default has some implications for programs
that want to run
on both platforms (mainframe and PC, for example). I usually cope with
this by explicitly
specifying unsigned char, wherever necessary (only at places, where
computing is done
with character values, for example, when doing hexadecimal output of
storage areas
on both platforms).
BTW: if you want to take a closer look at Pascal, you can find the New
Stanford Pascal Compiler here:
http://bernd-oppolzer.de/job9.htm
https://github.com/StanfordPascal
https://www.facebook.com/StanfordPascal
which runs on IBM Mainframes (MVS and CMS) and on Windows, Unix (Linux),
OS/2, MacOS etc.;
on the Mainframe, 370 machine code is generated. On the other platforms,
the generated P-Code
is interpreted. The P-Code interpreter PCINT is written in C and (of
course) has to use unsigned chars
to implement the P-instructions in exactly the same way as they are
compiled on the mainframe.
Kind regards
Bernd
Am 26.04.2020 um 23:44 schrieb Charles Mills:
Without testing, I think that a signed char argument satisfies and unsigned char
formal parameter, but that &signed char does not satisfy unsigned char *.
There is some C messiness
https://www.facebook.com/StanfordPascal/?eid=ARDEjQaZgVZL2fivkB0AXGk_-mZF97XV3mESzaJ0RR-2NKu0jyroGwFbXt9kqk5UUIUCiHV9d0w2XcfUaround
char signage. I think on most platforms char is kind of the same as signed
char, but not exactly the same. On Z a char is kind of the same as an unsigned
char, but not exactly the same. I am obviously fuzzy on the exact details, and
no doubt someone will be happy to set me straight.
Charles
----------------------------------------------------------------------
For IBM-MAIN subscribe / signoff / archive access instructions,
send email to [email protected] with the message: INFO IBM-MAIN