1

I bumped into byte datatype recently. My knowledge of C/C++ is shallow, but I know char and I couldn't find much on byte in C++ docs. I looked it up in the Arduino docs:

It’s recommended to only use char for storing characters. For an unsigned, one-byte (8 bit) data type, use the byte data type.

Is byte datatype a solely Arduino construct? If so, why is it recommended over char? Both are 8-bit so why would it make any difference?

  • 1
    char is configured as signed in compiler setting of Arduino Arduino cores – Juraj Mar 3 at 11:04
  • 1
    Whenever I make something bigger than just 50 lines, I always use the most specific types like: uint8_t, int8_t, uint16_t, int16_t, uint32_t and int32_t to know exactly what is signed or unsigned and if it is 8, 16 or 32 bits. And I use char for characters, and bool for booleans. In this case you never have to think what fits (or should fit) in a variable. – Michel Keijzers Mar 3 at 11:23
2

Is byte datatype a solely Arduino construct?

Yes. Much of the Arduino "language" is "bent" to look like Java, since it originated from Processing, which is Java. So we have data types the "look like" Java ones, including "byte" for "unsigned char" and "boolean" for "bool".

If so, why is it recommended over char?

Personally I don't ever recommend using it, since it's non-standard. Instead you should use uint8_t which is essentially the same thing, but portable.

It is recommended to use "hard signedness" data types for portability. The problem with char is that whether it is signed or not is entirely down to the compiler implementation. Some systems have it as signed, some as unsigned. By using byte (or better, uint8_t) you are always going to get an unsigned 8-bit value.

For just storing of binary data it doesn't really make any difference, but when it comes to doing mathematics with the stored data you may find you suddenly have negative values when you don't want negative values (a char might be storing your number as -128 to +127, not 0 to 255).

| improve this answer | |
  • Great explanation. I will stick to specific unit8_t in the future and will go back and refactor my "chars". I prefer to stick to C/C++ syntax than to wander into Java and have another language to be shallow at. – Arthur Tarasov Mar 3 at 11:50
0

Just a little add-on to Majenko's answer:

If a function has a char* parameter, it's usually expecting a zero-terminated text.

If a function has a byte* parameter, it's usually any binary data, eventually containing '\0' bytes. The buffer size needs to be an additional parameter in this case.

If there's nothing else to consider, why not use that for your variable definition too: Use char only if it contains an 8bit character. Or something like char text[]="hello"; For binary data it might be relevant if it's signed (int8_t) or unsigned (uint8_t).


"usually" implies of course that you should read the doc.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.