In plain C, by the standard there are three distinct "character" types:
- plain
char
which one's signedness is implementation defined. signed char
.unsigned char
.
Let's assume at least C99, where stdint.h
is already present (so you have the int8_t
and uint8_t
types as recommendable alternatives with explicit width to signed and unsigned chars).
For now for me it seems like using the plain char
type is only really useful (or necessary) if you need to interface functions of the standard library such as printf
, and in all other scenarios, rather to be avoided. Using char
could lead to undefined behavior when it is signed on the implementation, and for any reason you need to do any arithmetic on such data.
The problem of using an appropriate type is probably the most apparent when dealing for example with Unicode text (or any code page using values above 127 to represent characters), which otherwise could be handled as a plain C string. However the relevant string.h
functions all accept char
, and if such data is typed char
, that imposes problems when trying to interpret it for example for a display routine capable to handle its encoding.
What is the most recommendable method in such a case? Are there any particular reasons beyond this where it could be recommendable to use char
over stdint.h
's appropriate fixed-width types?