Sub topic 4
Choosing the Smallest Data Type That Fits
This chapter assumes that you can write basic Arduino sketches, use variables and arrays, and read values in the Serial Monitor. It also assumes an AVR-based board such as the Arduino Uno, where SRAM is only 2 KB. Choosing suitable data types saves SRAM and can make calculations clearer.
Why data types matter
A data type determines what values a variable can hold and how many bytes it uses. On a small board, a few large arrays or structs can consume most of the available SRAM.
For example, an int on an Uno uses 2 bytes and stores values from -32,768 through 32,767. An unsigned int also uses 2 bytes, but stores only nonnegative values from 0 through 65,535. If a counter never becomes negative, the unsigned form gives it a larger useful range without using more memory.
| Type | Bytes on Uno | Typical range | Suitable use |
|---|---|---|---|
| `bool` | 1 | `false` or `true` | A yes/no state |
| `int8_t` | 1 | -128 to 127 | A small signed value |
| `uint8_t` | 1 | 0 to 255 | A pin number, mode, or small counter |
| `int16_t` | 2 | -32,768 to 32,767 | A signed sensor reading |
| `uint16_t` | 2 | 0 to 65,535 | Milliseconds or ADC values |
| `int32_t` | 4 | About -2.1 billion to 2.1 billion | Large totals or timestamps |
The exact size of types such as int can differ between Arduino boards. Types with a suffix, such as uint16_t, have a specified width. Include <stdint.h> when you use these fixed-width names.
A practical selection process
Choose a type by checking the value's limits, not its current value.
- Determine whether the value can be negative.
- Find its minimum and maximum possible values.
- Choose the smallest type whose range includes both limits.
- Check how arithmetic will behave before assigning the result.
Suppose a device has four operating modes. A uint8_t is enough:
#include <stdint.h>
uint8_t mode = 0;
const uint8_t modeCount = 4;
void setup() {
Serial.begin(9600);
}
void loop() {
if (mode >= modeCount) {
mode = 0;
}
Serial.println(mode);
mode++;
delay(1000);
}The variable does not need to represent negative values or values above 255. Using uint8_t documents that fact and uses one byte.
Common errors with small types
A small unsigned type wraps around when it exceeds its maximum:
uint8_t count = 255;
count++;
// count is now 0This is not an error in the C++ language. It is the defined behavior of an unsigned integer. Test for the limit before incrementing if wrapping is not wanted.
Arithmetic can also produce an unexpected result before assignment. Two uint8_t values are commonly promoted to int during an expression, but larger unsigned types can still cause unsigned arithmetic. Make the intended type visible with a cast or a wider temporary variable.
uint16_t milliseconds = 60000;
uint16_t delayAmount = 1000;
uint32_t nextTime = (uint32_t)milliseconds + delayAmount;A uint32_t temporary prevents the addition from being constrained to a 16-bit result.
Memory savings in arrays
The benefit becomes larger in arrays. An array of 100 uint8_t values uses 100 bytes. The same array of uint32_t values uses 400 bytes. Select the element type from the table's actual range.
Exercise
A variable stores a battery percentage from 0 through 100. Which fixed-width type is suitable, and how many bytes does it use on an Uno?
Show answer
uint8_t is suitable because its range is 0 through 255. It uses 1 byte.