For C coders out there, there is a time in your programming life that you will come across this question: What data type to use? unsigned char? or uint8_t?
Let’s look at this example:
We have our struct. It is actually a placeholder of parsed stream packets. The packet stream could be, for example
0x01 0x04 0xFF
With that, our struct can be filled up with
header = 0x01 controlByte = 0x04 breakProtocol = 0xFF
So what do we have here? The header, controlByte and breakProtocol are byte placeholder. And the programmer from example 1 made a point that he is storing 1 byte.
There are rare cases that char is not equal to 1 byte just like Analog Devices 32-bit SHARC DSP. But it’s rare.
There’s nothing wrong with the code portrayed in example 1. However, giving the struct members with the type uint8_t is a self documentation to the code.
With uint8_t, the programmer have the intention that this placeholder accepts 1 byte (8 bits).
This is beneficial also to the one who reads the code because it is clear that the unit/type to store are numbers (uint8_t) and not characters (unsigned char).
Likewise, if the variable is expecting a character, it would be weird to have:
So it’s fitting to have
unsigned char vs uint8_t is a case-to-case basis. It depends on the intention of the programmer. It depends on what is actually stored on the variable. Though both have same size, but they differ on purpose.