3 ms·
> `uint8_t` and `char` are not guarenteed to be the same type Remember that `char`, `signed char` and `unsigned char` are the different types, even though `cha
by quietbritishjim 8y ago
> `uint8_t` and `char` are not guarenteed to be the same type
Remember that `char`, `signed char` and `unsigned char` are the different types, even though `char` takes the same range of values as either `signed char` or `unsigned char`.
The typedef `uint8_t` is usually set to `unsigned char`, not `char`, even on systems where `char` is unsigned. Partly this reflects the fact that `char` is usually used to represent actual characters, while the other two types are usually used to represent integers that take the same amount of memory as `char`. The standard technically does not allow `uint8_t` to be `char` [1], although this requires an extremely pedantic reading.
Anyway, if you replace `char` with `unsigned char` in your comment then it's correct. I believe all current major implementations typedef `uint8_t` to `unsigned char`, but that's not guaranteed and even old implementations of GCC had a different type. `unsigned char` satisfies the same relaxed aliasing rule as `char` [2] but `uint8_t` may not.
[1] https://stackoverflow.com/a/16002781 https://stackoverflow.com/a/16002781
[2] https://stackoverflow.com/a/40575162 https://stackoverflow.com/a/40575162
- saagarjha 8y ago> I believe all current major implementations typedef `uint8_t` to `unsigned char` That can't be right, because CHAR_BIT is not always 8.
- maccard 8y agoPractically speaking, for most people these days it is. POSIX specifies 8 bits and windows uses it. If you're working with a larger bit char, you're most likely well aware of it (offhand, some TI embedded chips are the only ones I know of, and even theyre rare!)
- quietbritishjim 8y agoIt depends on what you count as a "major implementation".