Data Types & Variables

Back to let’s code

Data Type

In computer science and computer programming, a data type (or simply type) is a set of possible values and a set of allowed operations on it. A data type tells the compiler or interpreter how the programmer intends to use the data. Below is a list of the data types commonly seen in Arduino, with the memory size of each in parentheses after the type name.

Note: signed variables allow both positive and negative numbers, while unsigned variables allow only positive values.

Data Type Description
boolean A Boolean holds one of two values, true or false. Each Boolean variable occupies one byte of memory.
byte A byte stores an 8-bit unsigned number, from 0 to 255.
char A data type that takes up one byte of memory that stores a character value. Character literals are written in single quotes like this: ‘A’ and for multiple characters, strings use double quotes: “ABC”.

However, characters are stored as numbers. You can see the specific encoding in the ASCII chart. This means that it is possible to do arithmetic operations on characters, in which the ASCII value of the character is used. For example, ‘A’ + 1 has the value 66, since the ASCII value of the capital letter A is 65.

Unsigned char  an unsigned data type that occupies one byte of memory. The unsigned char data type encodes numbers from 0 to 255.
word unsigned number from 0-65535
int 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 unsigned number from 0-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 signed number from -2,147,483,648 to 2,147,483,647
float Data type for floating-point number is a number that has a decimal point. Floating-point numbers are often used to approximate the analog and continuous values because they have greater resolution than integers.

Floating-point numbers can be as large as 3.4028235E+38 and as low as -3.4028235E+38. They are stored as 32 bits (4 bytes) of information.

void The void keyword is used only in function declarations. It indicates that the function is expected to return no information to the function from which it was called.

Constant

The “const” keyword stands for constant. It is a variable qualifier that modifies the behavior of the variable, making a variable “read-only”. This means that the variable can be used just as any other variable of its type, but its value cannot be changed. You will get a compiler error if you try to assign a value to a “const” variable.

Constants defined with the “const” keyword obey the rules of variable scoping that govern other variables, which we will explain later. This, and the pitfalls of using “#define”, makes the “const” keyword a superior method for defining constants and is preferred over using “#define”.

What is Variable Scope?

Variables in C programming language, which Arduino uses, have a property called scope. A scope is a region of the program and there are three places where variables can be declared. They are:

  • Inside a function or a block, which is called local variables.
  • In the definition of function parameters, which is called formal parameters.
  • Outside of all functions, which is called global variables.

Local Variables

Variables that are declared inside a function or block are local variables. They can be used only by the statements that are inside that function or block of code. Local variables are not known to function outside their own.

Global Variables

Global variables are defined outside of all the functions, usually at the top of the program. The global variables will hold their value throughout the life-time of your program.

A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration.