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?
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.
5 comments
is it possible to convert byte value into integer… and can we compare two byte values if yes then how we will do that from initialising to execution…
plzz help me out on this
An int can never be 8 bits as the C standard mandates that the minimum size for an int is 16 bits.
Also in C there are actually three “char” types.
char, signed char, and unsigned char.
For historical reasons, they are different. So while a char may default to being signed or unsigned, it is not the same as either “signed char” or “unsigned char”.
In fact you can even see this if you start to look closely at the assembler output of the avr gcc compiler.
There are certain loops that will generate different code (for each of the 3 types) if you declare the loop variable,
char vs signed char vs unsigned char.
I would discourage using the “cutsie” Arduino types like byte. and NEVER use the Arduino type “word” as that is defined to be “unsigned short” which is 16 bits on AVR and 32 bits on ARM and pic32. The “word” type is a total fail.
If you want your code to be portable, then always use the ANSI defined types from and avoid Arduino proprietary types.
There is no reason to use the “cutsie” types from Arduino when there are standard types.
That is why standard types were created and they offer more than just bit width choices as you can also specify other types that give the compiler clues for their use like minimum size or speed desires. These can help the compiler generate better or faster code. Something that the Arduino types cannot do.
One bit of useful information is that for small loops like a for loop a uint8_t or Arduino “byte” type will generate much better code on an 8 bit processor like the AVR; however, it can actually generate worse code on a larger processor.
That is where the other types in stdint can come into play.
If you were to use uint_fast8_t or uint_least8_t you would would not require a larger processor to be restricted to using only 8 bits which might use less efficient instructions to handle a smaller loop variable when the registers are larger.
That is the power of using the stdint types.
You can provider better information to the compiler so it can generate better code.
“Anyway, in Arduino, byte, uint8_t and unsigned short can be used interchangeably”
Shouldn’t that be “byte, uint8_t and unsigned char”?
Good summary :-)
Thanks for the clear explanation.
Gr Ernst
This helped. Thanks for posting!