How many char types are there in C?
There is one char
type. There are 3 small character types: char
, signed char
, unsigned char
. They are collectively called character types in C.
char
has the same range/size/ranking/encoding as signed char
or unsigned char
, yet is a distinct type.
- what is a plain char and how is it any different from signed char and unsigned char?
They are 3 different types in C. A plain char char
will match the same range/size/ranking/encoding as either singed char
or unsigned char
. In all cases the size is 1.
2 .how is myPlainChar - 'A'
different from mySignChar - 'A'
and myUnsignChar - 'A'
?
myPlainChar - 'A'
will match one of the other two.
Typically mySignChar
has a value in the range [-128...127] and myUnsignChar
in the range of [0...255]. So a subtraction of 'A'
(typically a value of 65) will result a different range of potential answers.
- Can someone please explain me the statement "Printable char's are always positive".
Portable C source code characters (the basic
execution character set) are positive so printing a source code file only prints characters of non-negative values.
When printing data with printf("%c", some_character_type)
or putc(some_character_type)
the value, either positive or negative is converted to an unsigned char
before printing. Thus it is a character associated with a non-negative value that is printed.
C has isprint(int c)
which "tests for any printing character including space". That function is only valid for values in the unsigned char
range and the negative EOF
. isprint(EOF)
reports 0. So only non-negative values pass the isprint(int c)
test.
C really has no way to print negative values as characters without undergoing a conversion to unsigned char
.