ATmega328P/How a sketch is written/08. Variables, and why int stops at 32767
How a sketch is written · 08 of 33

Variables, and why int stops at 32767

On this chip an int is 16 bits, so it holds -32768 to 32767 and nothing else. Add one to 32767 and you get -32768, with no error and no warning. Choosing a type is choosing the largest number the value will ever need.

Your board

A variable is a few bytes of SRAM

int counter = 32760; asks for a place in SRAM big enough for an int, names it counter, and puts 32760 in it. The type decides how many bytes that place has, and so the biggest number it can hold. The ATmega328P has 2048 bytes of SRAM for everything, so the sizes are small:

TypeBytesHolds
bool1true or false
byte10 to 255
char1-128 to 127; used for one letter
int2-32,768 to 32,767
unsigned int20 to 65,535
long4-2,147,483,648 to 2,147,483,647
unsigned long40 to 4,294,967,295
float4about seven significant digits, with a decimal point
double4the same as float on this chip

The sizes are the ATmega328P's, the same on the UNO and the Nano, from Arduino's language reference. On 32-bit boards int is 4 bytes and double is 8, which is why the same code can behave differently there.

One past the top

One count, four types
count = 32,767
Times the sketch has added 132,767
Jump to
Real count
32,767
int holds
32,767
unsigned int holds
32,767
int, unsigned int and long still agree; the byte has wrapped. 32767 is the last number a 16-bit int can hold, because one of its 16 bits is the sign.

A 16-bit int uses its top bit for the sign. 32767 is every other bit set; add one and the top bit is set instead, which the chip reads as -32768. An unsigned int has no sign bit, so it reaches 65535 before it runs out of bits and goes back to 0. A long, with 32 bits, still holds the true count.

The sketch on this page shows the same on the board. It prints:

int 32767 unsigned int 1 long 32767

int -32768 unsigned int 2 long 32768

Nothing stops it and nothing is reported. A sensor total or a timer held in an int turns negative this way, and the first sign is a value that makes no sense. For unsigned types the wrap is a rule of the language. For signed types C++ calls it undefined, which allows the compiler to do something else; on this chip what you see is the wrap.

Constants are ints too

A number written in the sketch, like 1000, is an int unless it says otherwise. So long ms = 60 * 1000; multiplies two ints, overflows at 60,000, and stores -5536 in a long that had room for the right answer. Put an L after one of them (60 * 1000L) and the sum is done in 32 bits.

float is approximate

A float stores about seven significant digits. 0.1 cannot be written exactly in binary, so sums of tenths drift in the last digit, and two floats that should be equal often are not. Compare them with a tolerance, or keep money and counts in whole numbers. Division between two ints throws the remainder away: 7 / 2 is 3, and 7 / 2.0 is 3.5.

The chip has no hardware for floating point, so every float sum is done in software, and it is far slower than the same sum in int. Use it when the number really has a fraction in it.

if, for and functions puts these variables to work.

The code

Int_Overflow.ino

Three counters start just below the top of their types and count up by one every half second. Open the Serial Monitor at 9600 baud and watch the first two go wrong within five seconds.

// Int_Overflow.ino — three counters, two of which wrap round
// Tools > Board: Arduino Uno, or Arduino Nano with
//   Processor ATmega328P (Old Bootloader if the upload says
//   "not in sync")
// Tools > Port: the CH340 port
// Serial Monitor: 9600 baud
// Wiring: none

int counter = 32760;             // 16 bits, signed
unsigned int ucounter = 65530;   // 16 bits, no sign
long lcounter = 32760;           // 32 bits, signed

void setup() {
  Serial.begin(9600);
}

void loop() {
  Serial.print("int ");
  Serial.print(counter);
  Serial.print("   unsigned int ");
  Serial.print(ucounter);
  Serial.print("   long ");
  Serial.println(lcounter);
  counter = counter + 1;
  ucounter = ucounter + 1;
  lcounter = lcounter + 1;
  delay(500);
}

unsigned int goes from 65535 to 0 after six lines; int goes from 32767 to -32768 after eight. long carries on. Press RESET to watch it again.

When it does not work

A number that only goes up suddenly turns negative

It is a signed type that has passed its top: an int after 32767, a long after 2147483647. Use a type with room for the largest value it will ever reach, unsigned long for anything counting milliseconds.

60 * 1000 comes out as -5536

Both numbers are ints, so the multiplication is done in 16 bits and overflows before it is stored, even into a long. Make one of them long: 60000L, or 60UL * 1000. The same bites any calculation with seconds, milliseconds or large sensor readings.

A float prints only two decimal places

Serial.print() rounds a float to two places unless told otherwise. Serial.println(x, 4) prints four. The number in the chip has about seven significant digits either way; more places than that print digits that mean nothing.

Code from another board gives different answers here

On 32-bit boards an int is 32 bits and a double is 64. On the ATmega328P, UNO and Nano alike, both are smaller: int is 16 bits and double is the same 32-bit number as float. Code written for those boards can overflow here. Types like int32_t and uint16_t name their size and mean the same on every board.

Where this goes next

Decisions, repetition and names for blocks of code, on the lamp on pin 13.

if, for and functions →

Edit this page — content/books/atmega328p/variables-and-types.mdx

Discuss this article

Ask about this page. The answer stays here, on the page it belongs to, for whoever hits the same wall next.

Browse Modules and blocks on the forum →