by caf · 11y ago · 0 comments
What? No. Use plain "char" for strings (which may be signed or unsigned, depending on the environment).
If you use "signed char" in an environment where plain "char" is unsigned then a comparison like:
string[i] == '\xff'
will be always false.In fact, when working in constrained memory environments, like embedded 8 bit applications a char will often be used to do math, and then it makes a big difference. This is because there is no byte type by default in C.
There is in C99 and newer: int8_t or uint8_t.
C99 is old enough to drive
I'm using that from now on.----
From a draft version of the C standard:
The typedef name intN_t designates a signed integer type with width N , no padding bits, and a two’s complement representation. Thus, int8_t denotes such a signed integer type with a width of exactly 8 bits.
The typedef name uintN_t designates an unsigned integer type with width N and no padding bits. Thus, uint24_t denotes such an unsigned integer type with a width of exactly 24 bits.
These types are optional. However, if an implementation provides integer types with widths of 8, 16, 32, or 64 bits, no padding bits, and (for the signed types) that have a two’s complement representation, it shall define the corresponding typedef names.