Every variable in an Arduino sketch has a type, which decides how much memory it uses and what values it can hold. Most of the time int just works. But a few type mistakes cause some of the strangest bugs in Arduino projects: timers that fire at the wrong moment, sums that go negative, and readings that stop at a round number. This guide shows each one with real output, so you can recognise it.
The main types
Sizes below are for the Arduino Uno R3, Nano and other 8-bit AVR boards.
| Type | Size | Range | Use it for |
|---|---|---|---|
bool | 1 byte | true or false | Flags: is the door open? |
byte / uint8_t | 1 byte | 0 to 255 | Pin numbers, brightness, raw bytes |
char | 1 byte | −128 to 127 | Text characters |
int | 2 bytes | −32,768 to 32,767 | General whole numbers, analogRead() results |
unsigned int | 2 bytes | 0 to 65,535 | Counts that never go negative |
long | 4 bytes | −2,147,483,648 to 2,147,483,647 | Big whole numbers |
unsigned long | 4 bytes | 0 to 4,294,967,295 | millis() and micros() times |
float | 4 bytes | ±3.4 × 1038, about 6 to 7 significant digits | Measurements with decimals |
double | 4 bytes on AVR | Same as float on an Uno | Extra precision, but only on 32-bit boards |
32-bit boards are different
On an ESP32, Raspberry Pi Pico, Arduino Uno R4 or other 32-bit board, int is 4 bytes, the same as long, and double is a true 8-byte double. Code that works there can overflow when moved to an Uno. If the size matters, use the fixed-width types, which are the same on every board: int16_t, uint16_t, int32_t, uint32_t and so on.
Overflow: when numbers wrap round
When a value goes past the top of its range, it doesn't stop or raise an error. It wraps round to the other end, like a car's mileometer rolling over. This sketch shows the classic cases. The comments are the real output on an Uno:
void setup() {
Serial.begin(9600);
int a = 300, b = 200;
long wrong = a * b; // multiplied as 16-bit ints first: overflows
long right = (long)a * b; // make one of them long, and it works
Serial.println(wrong); // -5536
Serial.println(right); // 60000
unsigned long oops = 60 * 1000; // 60 and 1000 are ints too
unsigned long ok = 60UL * 1000; // UL makes the first one unsigned long
Serial.println(oops); // 4294961760
Serial.println(ok); // 60000
int x = 32767;
x = x + 1;
Serial.println(x); // -32768: wrapped round
byte y = 255;
y = y + 1;
Serial.println(y); // 0
Serial.println(7 / 2); // 3: integer division throws away the .5
Serial.println(7 / 2.0); // 3.50
Serial.println(sizeof(int)); // 2 on an Uno, 4 on an ESP32
}
void loop() {}
The two that catch everyone:
- The answer's type doesn't help. In
long wrong = a * b;, the multiplication happens first, usingint, because both inputs areint. 60,000 doesn't fit, it wraps to −5,536, and only then is it stored in thelong. Cast one input tolongfirst. - Numbers you type are
inttoo.60 * 1000overflows before it reaches theunsigned long, and a one-minute timer becomes about 49.7 days. Write60UL * 1000or60000UL. The compiler often warns about this one, so read the warnings.
Integer division
Dividing two whole numbers gives a whole number, with the remainder simply thrown away, so 7 / 2 is 3. That bites when scaling sensor readings: analogRead(A0) / 1023 * 5 is 0 for every reading below 1,023. Make one of the numbers a decimal so the sum is done in float: analogRead(A0) * 5.0 / 1023.0.
Float precision
A float keeps only about 6 to 7 significant digits. Store 16,777,217 in one and it becomes 16,777,216.00, because that's the nearest value a float can hold. Small rounding errors also mean you should never test floats with ==; check whether they're close instead, for example if (fabs(t - 25.0) < 0.01). Floats are also much slower than whole numbers on an 8-bit chip, which has no floating-point hardware. For money-like or counting jobs, use whole numbers (pence, not pounds; millivolts, not volts).
millis() and the 49.7-day rollover
millis() returns an unsigned long, which reaches 4,294,967,295 after about 49.7 days and then rolls over to 0. Code written as "if (millis() >= nextTime)" breaks at that moment. Subtracting instead keeps working, because unsigned subtraction wraps correctly: if the last time was 4,294,967,000 and now is 200, now − last gives 496 ms, the right answer.
unsigned long previous = 0;
const unsigned long INTERVAL = 1000UL;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
}
void loop() {
if (millis() - previous >= INTERVAL) { // safe across the 49.7-day rollover
previous += INTERVAL;
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
}
Store every time from millis() in an unsigned long. Putting it in an int breaks after just 32.8 seconds. There's more on timing in millis() vs delay().
Choosing a type: a quick rule
- Times from
millis()ormicros():unsigned long, always. - Small values that are never negative (pins, 0 to 255 brightness):
byte. - Whole numbers that could pass 32,767:
long, and watch the maths in between. - Readings with decimals:
float, but don't compare with==. - Exact sizes for data sent between boards:
int16_t,uint32_tand friends.
Going deeper
Signed overflow is undefined behaviour
In C++ only unsigned types are guaranteed to wrap. Overflowing a signed int is formally "undefined behaviour": on AVR it wraps in practice, as shown above, but an optimising compiler is allowed to assume it never happens and can remove checks such as if (x + 1 < x). Don't rely on signed wrapping; use a bigger type or an unsigned one.
Integer promotion
Before doing arithmetic, C++ promotes anything smaller than int (such as byte and char) up to int. That's why byte y = 255; y + 1 is 256 inside the sum but 0 once stored back in y. Mixing signed and unsigned values converts the signed one to unsigned, so -1 < 1u is false. The compiler's -Wsign-compare warning flags this.
Two's complement
Negative whole numbers are stored in two's complement: invert all the bits and add one. In 16 bits, 0x7FFF is 32,767 and adding 1 gives 0x8000, which reads as −32,768. That's the wrap seen above. Our bitwise operations guide shows how the bits line up.
Memory on an Uno
The ATmega328P has just 2 KB of RAM. An array of 500 float readings needs 2,000 bytes and won't fit alongside anything else, whereas 500 int readings in tenths of a degree need 1,000. Text in Serial.print("...") also uses RAM unless you wrap it as F("..."), which keeps it in flash.
Sources
The main references behind this guide. Numbers we worked out ourselves, such as the code outputs, come from running the code.
- Arduino language reference: int (16-bit on ATmega boards, 32-bit on Due and SAMD boards)
- Arduino language reference: unsigned long
- Arduino language reference: float (6 to 7 digits of precision)
- Arduino language reference: double (same as float on ATmega boards)
- Arduino language reference: millis() (overflows after about 50 days)
- cppreference: Arithmetic operators (overflow, promotion and conversions)