In this C++ tutorial, you will learn how to find the length of an array using a range-based for loop, the sizeof operator, std::size(), and the size() member function available on standard containers.

What does array length mean in C++?

The length of an array is the number of elements stored in that array. For example, the array {3, 5, 8, 13, 21, 34} contains six elements, so its length is 6.

How you get that length depends on the kind of array or container you are using. A built-in C-style array does not have a length property or a size() member function. In modern C++, std::size(array) is usually the clearest option for a built-in array when C++17 or later is available. The traditional sizeof calculation also works while the object is still an actual array.

Find C++ array length using a range-based for loop

A range-based for loop visits each element in an array. If you increment a counter once per iteration, the final counter value is the number of elements. This method does not use sizeof.

1. Find length of Integer Array

In this example, we take an integer array and initialize a counter named len to zero. The range-based for loop runs once for each array element, and len is incremented during every iteration.

After the range-based for loop finishes, the counter contains the number of elements in the array.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   int arr[] = {3, 5, 8, 13, 21, 34};
   int len = 0;

   for (int n: arr)
      len++;

   cout << len;
}

Output

6

The loop runs six times, once for each element, so len becomes 6. If you only need the number of elements, however, std::size() or sizeof is more direct because neither needs to iterate through the array.

Find C++ array length using std::size()

From C++17, std::size() can return the number of elements in a built-in array. Include the <iterator> header and pass the array to std::size().

</>
Copy
std::size(array_name)

For example:

</>
Copy
#include <iostream>
#include <iterator>

int main() {
    int arr[] = {3, 5, 8, 13, 21, 34};

    std::cout << std::size(arr);
}

Output

6

This is concise and avoids manually dividing byte sizes. It also makes the intent clear: obtain the element count rather than the number of bytes occupied by the array.

Find C++ array length using sizeof

For a built-in array, sizeof(arr) gives the total number of bytes occupied by the array, while sizeof(arr[0]) gives the size of one element. Dividing the two values gives the number of elements.

</>
Copy
sizeof(array_name) / sizeof(array_name[0])

Example:

</>
Copy
#include <iostream>

int main() {
    int arr[] = {10, 20, 30, 40, 50};

    std::size_t len = sizeof(arr) / sizeof(arr[0]);

    std::cout << len;
}

Output

5

This calculation works because arr is still an array in the same scope. It should not be applied to a pointer that merely points to the first element of an array.

Why sizeof does not give array length after pointer decay

When a built-in array is passed to an ordinary function parameter such as int arr[], the parameter is adjusted to a pointer. Inside that function, sizeof(arr) is therefore the size of a pointer, not the size of the original array. The original element count is no longer available from that pointer alone.

</>
Copy
#include <iostream>

void printLength(int arr[]) {
    std::cout << sizeof(arr) / sizeof(arr[0]);
}

int main() {
    int arr[] = {10, 20, 30, 40, 50};
    printLength(arr);
}

Do not use this pattern to determine the array length. Depending on pointer and element sizes, it can produce a value unrelated to the actual number of elements.

Get C++ array length inside a function

If a function needs to know the size of a built-in array at compile time, accept the array by reference and let the compiler deduce its length as a template parameter.

</>
Copy
#include <iostream>
#include <cstddef>

template <typename T, std::size_t N>
constexpr std::size_t arrayLength(const T (&)[N]) {
    return N;
}

int main() {
    int arr[] = {10, 20, 30, 40, 50};

    std::cout << arrayLength(arr);
}

Output

5

The reference parameter preserves the array type, so the compiler can deduce N as 5. Another common design is to pass the element count as a separate argument, especially when a function accepts a pointer.

C++ array length without sizeof

If you want the length without using sizeof, choose a method that matches your C++ version and data type:

  • Use std::size(arr) for a built-in array in C++17 or later.
  • Use a range-based for loop and a counter if you specifically want to count elements by iteration.
  • Use std::array::size() for std::array.
  • Use std::vector::size() for a dynamically sized std::vector.

Length of std::array in C++

std::array is a fixed-size standard container. Unlike a built-in array, it provides the member function size().

</>
Copy
#include <array>
#include <iostream>

int main() {
    std::array<int, 4> arr = {10, 20, 30, 40};

    std::cout << arr.size();
}

Output

4

The return value of size() is an unsigned size type. For indexing or storing container sizes, std::size_t is commonly appropriate.

Length of std::vector in C++

If the number of elements can change at runtime, std::vector is usually a better fit than a built-in array. Use its size() member function to get the current number of elements.

</>
Copy
#include <iostream>
#include <vector>

int main() {
    std::vector<int> values = {10, 20, 30};

    values.push_back(40);

    std::cout << values.size();
}

Output

4

Use C++ array length in a for loop

The array length is often needed as the loop boundary. With C++17 or later, you can use std::size() directly in the condition.

</>
Copy
#include <iostream>
#include <iterator>

int main() {
    int arr[] = {4, 8, 12, 16};

    for (std::size_t i = 0; i < std::size(arr); ++i) {
        std::cout << arr[i] << '\n';
    }
}

Output

4
8
12
16

If you only need each value and not its index, a range-based for loop is simpler and avoids writing the array length explicitly.

Empty arrays and zero-length arrays in C++

2. Empty built-in array example and its portability

The following original example attempts to create a built-in array with no elements and then count it with a range-based for loop.

C++ Program

</>
Copy
#include <iostream>
using namespace std;

int main() {
   int arr[] = {};
   int len = 0;

   for (int n: arr)
      len++;

   cout << len;
}

Output

0

The code above is retained from the original tutorial, but a zero-length built-in array is not portable standard C++. Some compilers may accept it as an extension, while conforming C++ code should use a standard container when an empty sequence is required.

For a fixed-size empty container, std::array<int, 0> is standard C++ and reports a size of zero.

</>
Copy
#include <array>
#include <iostream>

int main() {
    std::array<int, 0> arr{};

    std::cout << arr.size();
}

Output

0

Choosing the correct C++ array length method

Data type or situationRecommended way to get the number of elements
Built-in array, C++17 or laterstd::size(arr)
Built-in array, older C++sizeof(arr) / sizeof(arr[0]), while arr is still an array
Built-in array passed to a functionPass by array reference with a deduced size, or pass the size separately
std::arrayarr.size()
std::vectorvalues.size()
Raw pointerThe element count cannot generally be recovered from the pointer alone; store or pass the count separately

Common mistakes when finding C++ array length

  • Looking for arr.length: built-in C++ arrays do not have a length property.
  • Calling arr.size() on a built-in array: that member function belongs to standard containers such as std::array and std::vector.
  • Using sizeof on a function parameter declared as an array: the parameter behaves as a pointer, so the original array size has been lost.
  • Confusing bytes with elements: sizeof(arr) returns the total byte size, not directly the number of elements.
  • Assuming a pointer stores array length: a raw pointer does not carry the number of elements it points to.

C++ array size references

For additional reference, see Microsoft Learn’s C++ arrays documentation and cppreference’s std::array::size reference.

Editorial QA checklist for C++ array length examples

  • Verify that every built-in array example distinguishes element count from byte size.
  • Verify that std::size() examples mention the C++17 requirement and include the needed header.
  • Verify that no example uses sizeof(pointer) / sizeof(pointer[0]) as an array-length calculation.
  • Verify that function examples either preserve the array type by reference or receive the element count separately.
  • Verify that std::array and std::vector examples use their size() member functions.
  • Verify that zero-length built-in arrays are not presented as portable standard C++.

C++ array length summary

For a built-in array, use std::size(arr) in C++17 or later, or use sizeof(arr) / sizeof(arr[0]) while the variable is still an actual array. Do not expect the same sizeof calculation to work after the array has decayed to a pointer. For std::array and std::vector, call size().

In this C++ Tutorial, we learned how to find the length of a C++ Array.