word vs unsigned int, when to use which ?

hello guys,
i am reading about the data types of arduino, i've encountered some data types that says are the same like word and unsigned int, byte and unsigned char, well i know that word and unsigned int are both 16 bit from unsigned from 0-65535, and same for byte which is 8 bit, my question is when to use word and when to use unsigned int and same for byte and unsigned char, if they are the same then, it's just a matter of which one we like so we use ?

one more thing, i was reading on sparkfun's tutorial on data types, they say that an int is 16 bit which mean 2 bytes, then i searched on google for C data types and found a link in which they say an int is 2 or 4 bytes ! so which info is right ? or does the arduino int differ from the original C/C++ type ?

Never use word. Use byte of unsigned char interchangably. Unsigned char is more portable since byte is only defined for Arduino.

Delta_G:
Never use word. Use byte of unsigned char interchangably. Unsigned char is more portable since byte is only defined for Arduino.

aha ! first why never use word ?! (i’ve never seen an example using it but i encountered it while reading)

second, byte is just for arduino and doesn’t exist in C and C++ ?!

First: word isn't portable.
Second: correct

Groove:
First: word isn't portable.

what do you mean isn't portable ?

what do you mean isn't portable ?

If, for example, you get your code working on the Arduino, but then decide you want to run it on a Raspberry Pi, too, you would want to use keywords that are found on both platforms so you can "port" (i.e., recompile) your program on a different processor without having to change the source code. If you use byte, no other C compiler is going to recognize it, so you would have to change byte to unsigned char on the non-Arduino platform. Given that fact, why not use unsigned char from the start so the code is portable from one platform to another?

econjack:
If, for example, you get your code working on the Arduino, but then decide you want to run it on a Raspberry Pi, too, you would want to use keywords that are found on both platforms so your a "port" (i.e., recompile) your program on a different processor without having to change the source code. If you use byte, no other C compiler is going to recognize it, so you would have to change byte to unsigned char on the non-Arduino platform. Given that fact, why not use unsigned char from the start so the code is portable from one platform to another?

ah great explanation thank you
well the thing is Byte is very obvious and easy to understand that's why i would prefer it on unsigned char when coding, but i am really surprised why byte doesn't exist in C/C++ !!

one more thing, i was reading on sparkfun's tutorial on data types, they say that an int is 16 bit which mean 2 bytes, then i searched on google for C data types and found a link in which they say an int is 2 or 4 bytes ! so which info is right ? or does the arduino int differ from the original C/C++ type ?

The size of an int depends upon the host processor and its architecture. If you port your code to a platform where byte is not supported, you could use:

#define byte unsigned char

at the top of the source code file on the new platform. Now when the new compiler sees the (undefined keyword) byte, it will substitute unsigned char for it. This is a dirty hack...I need a shower.

No dirty hacks necessary, use int8_t, uint8_t, int16_t … defined in <stdint.h>.

lol ok it's like defining a variable, smart hack :smiley:

about the uint8_t ... those are like saying undefined char, ... etc ?

Back in the 70's Kernighan and Ritchie had the worst idea ever. It was back when people were still mucking about with architecture. There were four bit machines, 8 bit machines, even an occasional 16 bit machine. Their idea was that in their new language, C, they would leave the size of the primitive data types in C undefined. That way compilers could choose whatever size was best for the architecture they were running on. If a machine is a 32-bit machine, why not just let the compiler use all 32 bits for an int?

And so it was.

Ever since that day, every piece of serious software written in C has had a massive include file with preprocessor directives whose entire job is to work out how to trick the compiler that it happens to be running on into making a variable 16 bits and only 16 bits.

firashelou:
lol ok it's like defining a variable, smart hack :smiley:

about the uint8_t ... those are like saying undefined char, ... etc ?

Yes u - unsigned
int - integer
8- 8bits
_t -type

On Arduino:
uint8_t = unsigned char
int8_t = char
uint16_t = unsigned int
int16_t = int
uint32_t = unsigned long
int32_t = long

Delta_G:
Yes u - unsigned
int - integer
8- 8bits
_t -type

On Arduino:
uint8_t = unsigned char
int8_t = char
uint16_t = unsigned int
int16_t = int
uint32_t = unsigned long
int32_t = long

aha ok thanks for the explanation, so it's just another way to represent the types or does it have any other meaning or purpose ?

PaulMurrayCbr:
Back in the 70's Kernighan and Ritchie had the worst idea ever. It was back when people were still mucking about with architecture. There were four bit machines, 8 bit machines, even an occasional 16 bit machine. Their idea was that in their new language, C, they would leave the size of the primitive data types in C undefined. That way compilers could choose whatever size was best for the architecture they were running on. If a machine is a 32-bit machine, why not just let the compiler use all 32 bits for an int?

And so it was.

Ever since that day, every piece of serious software written in C has had a massive include file with preprocessor directives whose entire job is to work out how to trick the compiler that it happens to be running on into making a variable 16 bits and only 16 bits.

so an 8 bit, would read and write very slow according to a 16bit or 32bit which would take less time to read and write ?

Back in the 70's Kernighan and Ritchie had the worst idea ever.

Not sure I agree. I think it makes a lot of sense and shows they weren't going to tie C to one platform. If you look at the grammar in the back of the K&R book, an int is not "undefined' in the strict sense of the word. Rather, they let the implementer of the compiler define the size that made sense for the host machine. Why is that such a bad idea, especially when ports normally go from smaller to larger register sizes?

it's just another way to represent the types or does it have any other meaning or purpose ?

Yes. The thing is, "int" may be 16, 32, or even 64bits, depending on which chip and compiler you are using, but "int16_t" is always a 16bit integer.

A lot of what I do on a 328P is hardware specific in multiple ways and a lot will port.
So what if I use 'word'? Wanna port that? Try search and replace in the editor.

What gets me is that AVR word length is 8 bits yet Arduino 'word' is 16.

Come to think of it, uint would do for a nice short name, the _16 part is a pain.
Byte, uint, ulong?

westfw:
Yes. The thing is, "int" may be 16, 32, or even 64bits, depending on which chip and compiler you are using, but "int16_t" is always a 16bit integer.

aha ! ok so it's a constant
thanks for reply :slight_smile:

GoForSmoke:
What gets me is that AVR word length is 8 bits yet Arduino 'word' is 16.

An AVR instruction word is 16 bits.

In C, "byte" is the greater of 8 bits or the smallest unit of memory available in a single access. On some 32-bit DSPs, a byte (as defined in C) is 32 bits.

Jerry