HOWTO · Arduino

How to Get Arduino Array Length Using the sizeof() Operator

Discover how to count elements in a fixed-size Arduino array with the sizeof() operator. Learn how byte sizes produce an element count, how to apply the formula to different types and dimensions, and why array lengths must be passed separately to functions.

On this page

A fixed-size array in Arduino can be counted with sizeof(array) / sizeof(array[0]) while the full array is in scope. The result is the number of elements, so you can use it as a loop bound. sizeof() measures bytes; the division converts the array’s total byte size into an element count.

Get an Arduino array’s length with sizeof()

For example, if values contains five integers, sizeof(values) / sizeof(values[0]) evaluates to 5. This works because values names the complete array at this point in the program. The same expression works for a fixed-size array of another element type because the denominator uses the size of that array’s first element.

Why the sizeof() division works

sizeof is a C++ operator, not a function. Applied to an array, it reports the number of bytes occupied by the entire array. Applied to array[0], it reports the bytes occupied by one element. Dividing the first size by the second gives the number of elements, provided both expressions refer to the actual array and its element type.

For instance, the expression also counts a fixed-size float array without needing to assume how many bytes a float occupies on a particular board. Both sizes are determined for the same type and target, and the ratio is the element count. Avoid hard-coding a byte size such as four: it is the element count that the loop needs.

Apply the formula to other array types and dimensions

The element type does not change the formula. For an array named readings, use sizeof(readings) / sizeof(readings[0]). This is useful when the initializer changes: the count follows the declared array instead of a separately typed number.

For a two-dimensional fixed-size array, divide the whole array size by the size of its first row to count rows. Divide the first row’s size by the size of its first element to count columns. For example, with int matrix[3][4], sizeof(matrix) / sizeof(matrix[0]) is the row count, and sizeof(matrix[0]) / sizeof(matrix[0][0]) is the column count. These expressions describe the declared dimensions; they do not discover a dynamically changing collection’s current length.

Handle array lengths when passing arrays to functions

When an array is passed as a function parameter, the parameter is treated as a pointer to its first element. Inside that function, sizeof(parameter) measures the pointer, not the caller’s complete array. Dividing it by sizeof(parameter[0]) therefore does not recover the original element count.

Pass the count as a separate parameter and use it as the loop bound. Calculate it at the call site, where the full array is still in scope: printValues(values, sizeof(values) / sizeof(values[0])). The function can then process exactly that many elements. Keep the count and array together when passing them through multiple functions so each function has the information it needs.

The sizeof formula applies to fixed-size arrays whose complete type is available at the expression. It does not track a runtime-changing or dynamically allocated collection. For those, keep or obtain the element count through the collection’s own length mechanism.

Use the length in a complete Arduino sketch

In an Arduino sketch, declare the fixed-size array at file scope or inside setup(), calculate its count in the same scope, and iterate from index 0 while the index is less than that count. This keeps each access within the declared array. The sketch below uses Arduino’s Serial on a board. When compiled as ordinary C++, a small local Serial stand-in lets the same setup() logic print to standard output.

#include <stddef.h>

#ifdef ARDUINO
#include <Arduino.h>
#else
#include <iostream>

class SerialPort {
public:
  void begin(unsigned long) {}
  void print(const char* value) { std::cout << value; }
  template <typename T>
  void println(const T& value) { std::cout << value << '\n'; }
};
SerialPort Serial;
#endif

int values[] = {10, 20, 30, 40, 50};
const size_t valueCount = sizeof(values) / sizeof(values[0]);

void setup() {
  Serial.begin(9600);
  for (size_t i = 0; i < valueCount; ++i) {
    Serial.println(values[i]);
  }
  Serial.print("Count: ");
  Serial.println(valueCount);
}

void loop() {
}

#ifndef ARDUINO
int main() {
  setup();
}
#endif
10
20
30
40
50
Count: 5

On a board, upload the sketch and open the Serial Monitor at 9600 baud to view the values and count. If you move the loop into a helper function, pass valueCount explicitly along with the array; the helper cannot recover the original array length from its pointer parameter.