[solved] Choice of variables for integers below 65,000

I need to work with a few integers in the range of zero up to a maximum of around +10,000 so bytes and chars will not do, while Long is a waste of memory. No negative numbers are required so the choice seems to be down to Word vs Unsigned Int.

In my usage scenario, I will be doing some maths operations on the variables throughout the program. I am a hobbyist, I only ever work with Arduino and I have no plans to distribute my code. This program is embedded in a custom built, one-off device.

I did a little research and I have read that Word should not be used due to "portability", but further explanation was either unclear or lacking, therefore....

Q1. What is meant when people speak of "portability" for the Word variable - and should I be concerned as it seems that in my usage, I don't need consider it - or do I?

Q2. Referring to the Arduino reference, when using Unsigned Int with some maths operations, it seems I need to take just a little care to ensure that all inputs and results are maintained as Unsigned Int, otherwise "unexpected" results may occur. With this is mind, it seems that using Word is slightly cleaner and simpler to code for - but am I missing some other potential pitfall of using Word?

In your case, when staying within Arduino land, if all you need is 0-10,000 use the data type ‘int’ (Arduino 8 bit microcontrollers).

This might be best if you need to test for <0

Number 'type's.

  • boolean (8 bit) - simple logical true/false, Arduino does not use single bits for bool
  • byte (8 bit) - unsigned number from 0 to 255
  • char (8 bit) - signed number from -128 to 127. The compiler will attempt to interpret this data type as a character in some circumstances, which may yield unexpected results
  • unsigned char (8 bit) - same as 'byte'; if this is what you're after, you should use 'byte' instead, for reasons of clarity
  • word (16 bit) - unsigned number from 0 to 65535
  • unsigned int (16 bit) the same as 'word'. Use 'word' instead for clarity and brevity
  • int (16 bit) - signed number from -32768 to 32767. This is most commonly what you see used for general purpose variables in Arduino example code provided with the IDE
  • unsigned long (32 bit) - unsigned number from 0 to 4,294,967,295. The most common usage of this is to store the result of the millis() function, which returns the number of milliseconds the current code has been running
  • long (32 bit) - signed number from -2,147,483,648 to 2,147,483,647
    float (32 bit) - signed number from -3.4028235E38 to 3.4028235E38. Floating point on the Arduino is not native; the compiler has to jump through hoops to make it work. If you can avoid it, you should. We'll touch on this later. Sparkfun

Just FYI, 'word' is a Microsoft C specific type.

larryd:
In your case, when staying within Arduino land, if all you need is 0-10,000 use the data type ‘int’ (Arduino 8 bit microcontrollers).

I have long since read all the statements made in the variable description listings from Sparkfun as well as many other sources (thanks), but none of them include the information that answers my two questions.

From Arduino reference;

Word - A word can store an unsigned number of at least 16 bits (from 0 to 65535).

To boil it down into one question, is there any reason for NOT using Word in this case? For example, are there unreferenced differences in the way they are processed?

Yes, I know I could simply use unsigned int - but why not use Word? Would it create problems? Why does it exist if "int" does exactly the same job? Did it simply become redundant due to code revisions in the dim distant past?

Just an inquisitive mind trying to understand the differences - if any.

aarg:
Just FYI, 'word' is a Microsoft C specific type.

Fair enough, but it appears in the Arduino reference material, hence the reason why I am asking.

No reason to avoid using it.

If you want to use ‘word’ and it works for you, do so.

I mainly use: ‘char’ and ‘byte’ and ‘int’ and ‘unsigned int’ and ‘long’ and ‘unsigned long’

If the code needs to be uploaded to a different platform and there is a problem with portability, it would be a simple fix to: ‘find all and replace with something else’.


Are you maybe looking for a problem that isn’t there ?


Some here like: ‘uint8_t’ and ‘uint16_t’ instead of ‘byte’ and ‘unsigned int’

If you want portability the types in stdint.h are the way to go...

int8_t
int16_t
int32_t
uint8_t
uint16_t
uint32_t

This is automatically included by the Arduino IDE in every sketch.

No more guessing about is “word” or “int” 16 or 32 bit on different platforms, or is it signed or unsigned? All the information you need is right there in the type name.

Yes, Arduino designers made all kinds of subtle and not-so-subtle add ons, for example "boolean" when there is already a "bool" type. My best guess is that they added "word" for compatibility with Microsoft derived code. If you want to know why Microsoft did it, it is because of it's historical close association with the x86 processor, which uses the same terminology. But it isn't standard C/C++ so it is not advisable to use it, in case you need to port your code out of the Arduino environment. Yes it is true that you can change it, but why should you have to? Also a global search and replace on "word" seems pretty dangerous to me. "uint16_t" will function identically with "word" and it is a standard type that you can always depend on.

If it seems petty to you, realize that I develop with multiple IDE's for multiple platforms. I've found that standardization is generally a blessing, and there is no way to predict which standardizations are going to be the most important in the future. So I remain thankful for all of them and try to convince newcomers of the value in it, whenever a poignant example shows up.

Also there is no semantic help to the term, "word". It doesn't really help you remember what it represents. Whereas, "unsigned" really screams out the nature of it. So really, what is the point?

larryd:
No reason to avoid using it.

If you want to use ‘word’ and it works for you, do so.

Are you maybe looking for a problem that isn’t there ?

Thank you, that answers my questions.

As for the "problem" it only existed because while researching, I found some similar questions on this forums where the answers were along the lines of, "avoid using it" or words to that effect without adequate explanation.

As long is there is no chance of it causing an error then I'm fine with it.

Your good to go. :wink:

My best guess is that they added "word" for compatibility with Microsoft derived code.

Meh. Various C compilers have used "word" and "byte", or "WORD" and "BYTE" or various other names for a long time.
While they're a bit ugly, the stdint.h types (int16_t, etc) are part of the C standard for the last couple of decades... (Note that "word" gives no indication whether the number is signed or not...)

For me, the problems with "word" are

  1. the association with Micro$oft
  2. the meaninglessness of the word (no pun intended). The AVR is an eight bit micro, so the word size is eight bit, but the instructions are sixteen bit. With gcc and AVR, an "int" is sixteen bits - but I use Arm and ESP architectures too. So, no, I don't use "word"