When to use byte, int, longunsigned etc..

I'm probably going to get shot down for this but, as a beginner, is there any guidance on when to use byte, const byte, int, longunsigned etc etc... for variables?

It seems there are many different ways of doing the same thing?

It depends of range of values you are going to store at variable.

If you google Arduino variables you will get a definition of which does what - the range of numbers they can contain, decimal numbers and positive and negative numbers and so on.
You will see that some variable type use more memory than others and are also , therefore slower.
A a general rule use the variable that does what you want , but uses the minimum memory.

Bear in mind too , that digital inputs are single bits, and analogs in the range 0-1023 , which corresponds to an integer ( which uses 2 bytes).

This is worth a read :

Use the smallest data type for the situation. That is sometinmes determined by the value that a function returns. For instance, the millis() function returns unsigned long so putting the return value into an int data type is a mistake.

If you don't want the value of a variable to ever change, use the const modifier. For example naming a pin. You don't want the pin number to change in your code and you know that there are less than 255 pins use

const byte pin = 5;

That way you use the smallest data type and if you try to change the pin number in your code there will be an error reported.

Another modifier is unsigned for variables that will never be negative (as in unsigned long for timing variables for example).

hammy:
Bear in mind too , that digital inputs are single bits,

...but digitalRead returns an int, because it not possible to return a single bit

Fair point AWOL thx

...but you'd kinda hope that they'd have chosen byte instead :frowning:

.but you'd kinda hope that they'd have chosen byte instead

Me, too. Instead of wasting 15 bits only waste 7. And when is a bit negative?

877:
It seems there are many different ways of doing the same thing?

If you need the datatype to be portable or have a specific size, use something from <stdint.h> like unit8_t or int32_t.

If you want what the compiler developers believe is the most efficient datatype for the processor (the “native” datatype) use int or unsigned int.

If you need to perform math that could overflow the native datatype use long or unsigned long.

If the real-world values cannot be negative (like time elapsed) then use an unsigned datatype to gain an extra bit of precision. Otherwise use a signed datatype.

If you need to perform math that could overflow long / unsigned long then use float.

If the real-world value is text then use an array of char.

Six. There are six ways.

Darn my long reply got lost for some reason.

But thanks guys for all the info, I will check the links provided :slight_smile:

@Coding Badly I’ve been reading and count 18 data types? I probably misunderstand :slight_smile:

https://www.arduino.cc/reference/en/#variables

877:
@Coding Badly I’ve been reading and count 18 data types? I probably misunderstand :slight_smile:

You do appear to misunderstand the purpose of a question mark. It is your eyeballs and brain doing the counting. Only you can say if you did or did not count 18 things.

I can’t tell if you are joking, I hope so!

A bit of joking. A bit of sarcasm.

The point is: Broad questions are OK. Detailed questions are good. Non-questions ending with a question mark aren't worth expending the tiny amount of energy needed to write them.

Glad to hear it. I am very sarcastic myself but it does not translate well in written form so I limit it.

You said there are six in your earlier post. The link I posted shows eighteen data types, which is why I replied.

I know perfectly well how to use punctuation, I was attempting to soften my statement as I did not want to appear rude and contradict you.

Anyway, no problems. I don’t want to pop my 100 post cherry debating :slight_smile:

It's ... complicated.

The most important thing is to not use a data type that is too limited in range for the values you are dealing with, like:

byte voltage = analogRead();    // (analogRead returns a 10bit value in a 16bit "int."  it won't fit in a byte.
int fiveMinutes = 5*60*1000;    // 300000 doesn't fit in 16bits

Other efforts, like: "use the smallest type that will hold the value", "use const/static/volatile", "be careful of signed/unsigned", "use bool when appropriate", "use the explicitly-sized types like uint8_t"
are essentially optimization, clarifications, and semantic niceties that are less critically important, and perhaps not necessary for beginners.

The #variables arduino documentation lists a bunch of useful "types", but it lists various "aliases" that are the same thing (bool and boolean, or "byte" and "unsigned char", for example), mixes in some other concepts (arrays, "void"), and leaves other things out ("long long", the useful intN_t types)...

Thank you westfw for the explanation. It does indeed seem quite complicated!

I’ll try and always remember to use a data type that is non limiting.

Thanks again :slight_smile:

Data type discussion:

https://learn.sparkfun.com/tutorials/data-types-in-arduino/all

Nice thanks larryd :slight_smile: