Datatype Practices for Arduino Author: Alexander Brevig Contact: alexanderbrevig@gmail.com
This resource is subject to personal preference, but is based upon what seems to be a general consencus
A data type in programming languages is an attribute of data which tells the computer (and the programmer) something about the kind of data it is.
This involves setting constraints on the datum, such as what values it can take and what operations may be performed upon it.
Wikipedia
The number one concern on a µCU is size.
Datatype | RAM usage |
void keyword | N/A |
boolean | 1 byte |
char | 1 byte |
unsigned char | 1 byte |
int | 2 byte |
unsigned int | 2 byte |
word | 2 byte |
long | 4 byte |
unsigned long | 4 byte |
float | 4 byte |
double | 4 byte |
string | 1 byte + x |
array | 1 byte + x |
enum | N/A |
struct | N/A |
pointer | N/A |
Because all data needs to be stored, the selection of how to store data makes an impact on performance.
My general rule of thumb is 'select the smallest variable that are large enough, and make the datatype unsigned where suited'
I would never use an int to hold the digital pin on an Arduino. The int ranges from -32,768 to 32,767, and is clearly too large a container to store a number that never gets larger than 54.
The byte however is perfect. It ranges from 0 to 255, which is plenty of headroom.
Part of AlphaBeta Resources. | |
Last Modified: | July 17, 2009, at 05:28 PM |
By: | AlphaBeta |