Arduino Playground is read-only starting December 31st, 2018. For more info please look at this Forum Post

Datatype Practices for Arduino
Author:  Alexander Brevig
Contact: alexanderbrevig@gmail.com

Datatype Practices

<<under construction>>

This resource is subject to personal preference, but is based upon what seems to be a general consencus


Navigation


What is a datatype and what differentiates datatypes?

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.

Arduino reference lists these datatypes:

DatatypeRAM usage
void keywordN/A
boolean1 byte
char1 byte
unsigned char1 byte
int2 byte
unsigned int2 byte
word2 byte
long4 byte
unsigned long4 byte
float4 byte
double4 byte
string1 byte + x
array1 byte + x
enumN/A
structN/A
pointerN/A


Why do datatype practices affect sketches?

Because all data needs to be stored, the selection of how to store data makes an impact on performance.


How to know what datatype to select?

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.


Links


Information about this page

Part of AlphaBeta Resources.
Last Modified: July 17, 2009, at 05:28 PM
By: AlphaBeta