A variable of type int in Arduino C uses 2 bytes of data. Unless a sketch becomes very memory hungry, then ints tend to be used for almost everything, even for Boolean values and small integers that could easily be represented in a single byte value.

  TypeMemory (bytes)  Range  Notes
boolean1true or false (0 or 1)Used to represent logical values.
char1–128 to +127Used to represent an ASCII character code; for example, A is represented as 65. Negative numbers are not normally used.
byte10 to 255Often used for communicating serial data, as a single unit of data. See Chapter 9.
int2–32768 to +32767These are signed 16 bit values.
unsigned int20 to 65536Used for extra precision when negative numbers are not needed. Use with caution as arithmetic with ints may cause unex- pected results.
long42,147,483,648 to 2,147,483,647Needed only for representing very big numbers.
unsigned long40 to 4,294,967,295See unsigned int.
float4–3.4028235E+38 to + 3.4028235E+38Used to represent floating point numbers.
double4as floatNormally, this would be 8 bytes and higher precision than float with a greater range. However, on Arduino double is the same as float.
Data Types in Arduino C

The data types described in Table all apply to the original Arduinos based on the ATMega family of eight bit microcontrollers. With the advent of new ARM-based 32-bit microcontrollers, both official and compatible, these definitions do not necessarily stand for some devices. For this rea- son, some people prefer to use an alternative and more rigorous set of type definitions.

These are prefixed by the letter “t.” So, for example if you are expecting a 16-bit integer and want to make sure you don’t accidentally get a 32-bit integer, you can define your variable like this:

int16_t myVariable = 0;

To my mind, this looks ugly and makes the code less accessible. So, I only use these data type definitions, if it is critical that I get exactly the number of bits that I need. This is something that does not happen often, because if your variable ends up being bigger than expected because the processor processes that number of bits at a time, then the extra bits will simply be ignored. It only tends to be significant if you are doing some- thing a bit naughty, like relying on an integer to “wrap-around.”

Leave a Reply