Introduction
When declaring a variable, you need to state a data type. A data type determines how much space a variable can occupy in memory and what type of data.
Dynamic vs static typing
If you come from a background of Java, JavaScript, PHP, Python or Ruby – then this may seem odd. These languages are known as dynamically typed, which means that you do not need to state a data type. Whereas C++ is a statically typed language, meaning that you do need to state a data type.
void
I started with void as some people don’t count it as a data type! Whatever opinion you form, it needs to be covered, so I have included it here. void is a keyword that is used in function declarations to indicate that no information is expected to be returned to the function. Therefore there is no data being returned, leading some to say it cannot be a data type!
Boolean
A Boolean can hold one of two values, true or false. A Boolean occupies 1 byte in memory.
bool valOne = true;
bool valTwo - false;
Char
Char is used to store character values. For character literals you use single quotes and for multiple characters you use double quotes. The unique thing about char is that characters are stored as numbers. This means that you can perform arithmetic operations on characters! A char occupies 1 byte of memory.
char valOne = 'a';
char valTwo - 90;
byte
A byte is used to store an 8-bit unsigned number that is from 0 – 255.
byte valOne = 0;
int
int is used to store numbers, which can range from -32,768 to 32,767.
int valOne = 89;
ATTENTION!
Some readers who are coming from a C++ background and understand about data types maybe confused, as the sizes of some data types in Arduino will be different from that of a C++ environment on a Windows machine. The size capability of each data types is affected by the platform that is being used. An int on an Arduino will have a different size to an int on a Windows machine.
I wrote two programs, one to show the sizes of all of the different data types when using an Arduino and one for a Windows environment. Here are the results:
From these results you can see that an int and unsigned int is 2 bytes on my Arduino but 4 bytes on my Windows machine. A long and unsigned long is 4 bytes on my Arduino and 8 bytes on my Windows machine. A double is 4 bytes on my Arduino and 8 bytes on my Windows machine.