How to get the number of elements of an Array in Arduino
Learn how to find the length or number of elements of an array in Arduino using sizeof and simple formulas.
To get the number of elements in an Arduino array, use sizeof(array) / sizeof(array[0]). The sizeof() operator returns the total bytes the array occupies in memory, and dividing by the byte size of a single element gives the element count.
Quick Reference
| What You Need | Code | Returns |
|---|---|---|
| Number of elements | sizeof(arr) / sizeof(arr[0]) | Element count |
| Total memory used | sizeof(arr) | Bytes |
| Size of one element | sizeof(arr[0]) | Bytes |
It is common to use arrays as part of the code we upload to Arduino. You may have declared an array without explicitly defining its size, populating it directly:
int digitalPins[] = {2, 3, 4, 5, 6};At some point, you want to iterate over each element using a for loop. That means you need to know the total number of elements:
for (int i = 0; i < totalDigitalPins; i++) {
// apply logic to digitalPins[i]
}Your first instinct might be to use sizeof() directly:
int digitalPins[] = {2, 3, 4, 5, 6};
int totalDigitalPins = sizeof(digitalPins); // Returns 10, not 5!
for (int i = 0; i < totalDigitalPins; i++) {
// This will go out of bounds!
}This does not work correctly because sizeof() returns bytes, not the number of elements. In this article, we will cover the correct formula, explain why it works, show how to handle 2D arrays, and discuss common mistakes to avoid.
How to Get the Number of Elements in an Arduino Array
Arduino uses C++, and the formula to get the array length in C++ is to divide the total byte size of the array by the byte size of a single element:
Array length = sizeof(array) / sizeof(array[0])
Applying this to our example:
int digitalPins[] = {2, 3, 4, 5, 6};
int totalDigitalPins = sizeof(digitalPins) / sizeof(digitalPins[0]);
// totalDigitalPins = 10 / 2 = 5
for (int i = 0; i < totalDigitalPins; i++) {
// apply logic to digitalPins[i]
}This works because:
sizeof(digitalPins)returns 10 bytes (5 elements × 2 bytes perinton Arduino Uno)sizeof(digitalPins[0])returns 2 bytes (the size of oneint)- 10 / 2 = 5 elements
How Arduino sizeof() Works
The sizeof() operator is built into C/C++ and returns how many bytes a variable, data type, or array occupies in memory. It is evaluated at compile time, not at runtime, so it adds no overhead to your program.
sizeof() with Different Data Types
Different data types use different numbers of bytes. The exact sizes depend on the board you are using:
| Data Type | Arduino Uno/Nano (AVR) | Arduino Due / ESP32 |
|---|---|---|
bool / byte / char | 1 byte | 1 byte |
int | 2 bytes | 4 bytes |
long | 4 bytes | 4 bytes |
float / double | 4 bytes | 4 bytes / 8 bytes |
| Pointer | 2 bytes | 4 bytes |
This matters for array length calculations. The same formula works on every board, but the raw byte counts will differ. For example, an int array with 5 elements uses 10 bytes on an Uno but 20 bytes on an ESP32. The formula handles this automatically because both the numerator and denominator scale together.
You can verify these sizes on your own board using the Serial Monitor:
void setup() {
Serial.begin(9600);
Serial.print("bool: "); Serial.println(sizeof(bool));
Serial.print("char: "); Serial.println(sizeof(char));
Serial.print("int: "); Serial.println(sizeof(int));
Serial.print("long: "); Serial.println(sizeof(long));
Serial.print("float: "); Serial.println(sizeof(float));
Serial.print("double: "); Serial.println(sizeof(double));
}
void loop() {}
Serial Monitor output showing byte sizes of different data types on Arduino Uno
sizeof() with Arrays
When applied to an array, sizeof() returns the total bytes used by all elements combined:
int digitalPins[] = {2, 3, 4, 5, 6};
char letters[] = {'a', 'r', 'd', 'u', 'c'};
void setup() {
Serial.begin(9600);
Serial.print("digitalPins bytes: ");
Serial.println(sizeof(digitalPins)); // 10 (5 × 2 bytes)
Serial.print("letters bytes: ");
Serial.println(sizeof(letters)); // 5 (5 × 1 byte)
}
void loop() {}
An int array with 5 elements uses 10 bytes on Arduino Uno
Both arrays have 5 elements, but sizeof() returns different values because int uses 2 bytes per element while char uses 1 byte. This is exactly why you need the division formula — sizeof() alone does not give you the element count.

A char array with 5 elements uses only 5 bytes
Getting the Size of a 2D Array in Arduino
The same principle extends to multi-dimensional arrays. For a 2D array (a matrix), you can calculate both the number of rows and the number of columns:
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int rows = sizeof(matrix) / sizeof(matrix[0]); // 3
int cols = sizeof(matrix[0]) / sizeof(matrix[0][0]); // 4
int totalElements = rows * cols; // 12Here is how the math works on an Arduino Uno:
sizeof(matrix)= 24 bytes (3 rows × 4 columns × 2 bytes perint)sizeof(matrix[0])= 8 bytes (4 columns × 2 bytes perint)sizeof(matrix[0][0])= 2 bytes (oneint)- Rows = 24 / 8 = 3
- Columns = 8 / 2 = 4
A practical example — iterating over a 2D LED pattern:
// LED patterns: each row is a frame, each column is a pin
int patterns[][4] = {
{HIGH, LOW, LOW, HIGH},
{LOW, HIGH, HIGH, LOW},
{HIGH, HIGH, LOW, LOW},
};
int ledPins[] = {2, 3, 4, 5};
int numFrames = sizeof(patterns) / sizeof(patterns[0]);
int numPins = sizeof(patterns[0]) / sizeof(patterns[0][0]);
void setup() {
for (int p = 0; p < numPins; p++) {
pinMode(ledPins[p], OUTPUT);
}
}
void loop() {
for (int f = 0; f < numFrames; f++) {
for (int p = 0; p < numPins; p++) {
digitalWrite(ledPins[p], patterns[f][p]);
}
delay(500);
}
}Common Mistakes with sizeof() in Arduino
Mistake 1: Using sizeof() Inside a Function
This is the most common and dangerous pitfall. When you pass an array to a function, C++ converts it to a pointer. The function receives the memory address, not the actual array. So sizeof() inside the function returns the pointer size (2 bytes on Uno, 4 bytes on ESP32) — not the array size.
// ❌ WRONG — sizeof() returns pointer size, not array size
void printArray(int arr[]) {
int len = sizeof(arr) / sizeof(arr[0]); // Returns 1 on Uno!
for (int i = 0; i < len; i++) {
Serial.println(arr[i]);
}
}
// ✅ CORRECT — pass the length as a separate parameter
void printArray(int arr[], int len) {
for (int i = 0; i < len; i++) {
Serial.println(arr[i]);
}
}
void setup() {
Serial.begin(9600);
int myArray[] = {10, 20, 30, 40, 50};
int arrayLength = sizeof(myArray) / sizeof(myArray[0]);
printArray(myArray, arrayLength); // Pass length explicitly
}
void loop() {}Rule of thumb: always calculate the array length before passing the array to a function, then pass the length as an extra parameter.
Mistake 2: Forgetting to Divide by Element Size
// ❌ WRONG
int totalPins = sizeof(digitalPins); // Returns bytes, not elements
// ✅ CORRECT
int totalPins = sizeof(digitalPins) / sizeof(digitalPins[0]);With a char or byte array this mistake happens to produce the right answer (since each element is 1 byte), which makes it even harder to catch. Always use the full formula regardless of data type.
Mistake 3: Assuming All Boards Use the Same Byte Sizes
Code that works on an Arduino Uno may behave differently on an ESP32 or Arduino Due because int is 2 bytes on AVR boards but 4 bytes on 32-bit boards. The sizeof() division formula handles this automatically, but if you hardcode byte sizes (like dividing by 2 instead of sizeof(arr[0])), your code will break on different boards.
// ❌ WRONG — assumes int is always 2 bytes
int len = sizeof(myArray) / 2;
// ✅ CORRECT — works on any board
int len = sizeof(myArray) / sizeof(myArray[0]);Why Why Standard Arduino C++ Does Not Provide a Built-In Array Length Function
If you have worked with other programming languages like JavaScript, Python, or Java, you are used to getting the array length with a simple property or method:
// JavaScript
const myArray = [1, 2, 3, 4, 5];
const length = myArray.length; // 5
// Python
my_list = [1, 2, 3, 4, 5]
length = len(my_list) # 5Standard Arduino C++ does not provide this because of how these languages were designed. C was created in an era when computers had very limited memory. Storing metadata like the array length alongside every array would cost extra memory — memory that embedded systems like Arduino cannot afford to waste.
Additionally, C/C++ arrays are fixed-size. You cannot append elements to an array after declaring it (unlike JavaScript or Python). Since the size never changes at runtime, the language designers decided that compile-time calculation with sizeof() was sufficient.
Best Practice: Define the Array Size in a Variable
The simplest way to avoid the sizeof() calculation entirely is to define the array size in a separate variable or constant before declaring the array:
const int NUM_PINS = 5;
int digitalPins[NUM_PINS] = {2, 3, 4, 5, 6};
for (int i = 0; i < NUM_PINS; i++) {
pinMode(digitalPins[i], OUTPUT);
}This approach is clearer, safer (no pointer decay issues), and works reliably when passing arrays to functions. Use sizeof() when the array is populated inline and the number of elements is not known in advance.
Creating a Reusable ARRAY_LENGTH Macro
If you use the sizeof() formula frequently, you can define a macro to avoid repeating it:
#define ARRAY_LENGTH(arr) (sizeof(arr) / sizeof(arr[0]))
int digitalPins[] = {2, 3, 4, 5, 6};
char letters[] = {'a', 'b', 'c', 'd', 'e'};
void setup() {
Serial.begin(9600);
Serial.println(ARRAY_LENGTH(digitalPins)); // 5
Serial.println(ARRAY_LENGTH(letters)); // 5
}
void loop() {}Important: this macro has the same pointer decay limitation as raw sizeof(). It will not work correctly inside a function that receives the array as a parameter. Use it only in the scope where the array is originally declared.
Practical Example: Blinking Multiple LEDs
Here is a real-world example that uses sizeof() to dynamically iterate over an array of LED pins connected to an Arduino. This way, you can add or remove pins from the array without changing any other code:
/*
Blink Multiple LEDs using Arduino
https://thecircuitmaker.com/arduino-array-length
*/
int ledPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};
int totalPins = sizeof(ledPins) / sizeof(ledPins[0]);
int delayTime = 1000;
void setup() {
for (int i = 0; i < totalPins; i++) {
pinMode(ledPins[i], OUTPUT);
}
}
void loop() {
blinkOneAtATime();
}
void blinkAllLeds() {
for (int i = 0; i < totalPins; i++) {
digitalWrite(ledPins[i], HIGH);
}
delay(delayTime);
for (int i = 0; i < totalPins; i++) {
digitalWrite(ledPins[i], LOW);
}
delay(delayTime);
}
void blinkOneAtATime() {
for (int i = 0; i < totalPins; i++) {
digitalWrite(ledPins[i], HIGH);
delay(delayTime);
digitalWrite(ledPins[i], LOW);
delay(delayTime);
}
}
void blinkRandomLED() {
int randomIndex = random(totalPins);
digitalWrite(ledPins[randomIndex], HIGH);
delay(delayTime);
digitalWrite(ledPins[randomIndex], LOW);
delay(delayTime);
}Notice how totalPins is calculated once at the top using the sizeof() formula. If you add a new pin to the ledPins array, the rest of the code automatically adapts — no need to update a hardcoded number.
Summary
In Arduino (C++), there is no built-in .length() property for arrays. To get the number of elements, use:
int length = sizeof(array) / sizeof(array[0]);Key points to remember:
sizeof()returns bytes, not elements- Always divide by
sizeof(array[0]), not a hardcoded number - The formula does not work inside functions that receive the array as a parameter (pointer decay)
- For 2D arrays, use the same approach to get rows and columns separately
- When possible, define the array size as a
constvariable for clarity
If you are working on a project with multiple LEDs or blinking patterns, the sizeof() formula lets you add or remove array elements without touching the rest of your code. To learn the electrical fundamentals behind your Arduino projects, see our guides on Ohm's Law, voltage drop across resistors, and building a simple LED circuit.