Arduino: Difference In "Byte" VS "uint8_t" VS "unsigned Char"

10.6K

Byte, uint8_t and unsigned char, they are basically the same thing in Arduino. These data types often cause confusions to new programmers. So is there any difference in them?

sponsor-banner sponsor-banner Some of the links on this page are affiliate links. I receive a commission (at no extra cost to you) if you make a purchase after clicking on one of these affiliate links. This helps support the free content for the community on this website. Please read our Affiliate Link Policy for more information.

A byte stores an 8-bit unsigned number, from 0 to 255. For example for the number 0, the binary form is 00000000, there are 8 zeros (8 bits in total). for the number 255, the binary form is 11111111.

A uint8_t data type is basically the same as byte in Arduino. Writers of embedded software often define these types, because systems can sometimes define int to be 8 bits, 16 bits or 32 bits long. The issue doesn’t arise in C# or Java, because the size of all the basic types is defined by the language.

An unsigned char data type that occupies 1 byte of memory. It is the same as the byte datatype. The unsigned char datatype encodes numbers from 0 to 255. For consistency of Arduino programming style, the byte data type is to be preferred.

Buy the Arduino from: Banggood | Amazon

What are the difference? Which one should I use?

Not really, but It documents your intent for that variable. For example you will be storing small numbers, rather than a character, you will use byte.

Also it looks nicer to use uint8_t if you are also using other similar typedefs such as uint16_t or int32_t. With the number in the type definition, it clearly states how many bits the variable would take up. With or without the “u” at the beginning also gives people an idea it’s signed or unsigned.

The other thing that trips people up is “char”.  It can be equivalent to uint8_t or int8_t.  Or it might not be 8-bits at all, but that’s fairly rare. On Arduino, char is int8_t but byte is uint8_t.

Anyway, in Arduino, byte, uint8_t and unsigned short can be used interchangeably because they are literally the same type.  It’s just an alias. If you are just compiling the sketch on Arduino IDE and upload to the Arduino, use byte should be enough.

 

 

arduino

Tag » Arduino Byte Data Type Char