← Back to C++ Course | Chapter 6: Arrays & Strings | Lesson 4 of 15

C++ Array Size

An array's size is fixed at declaration, and sizeof combined with dividing by one element's size is the standard C++ way to compute how many elements it holds, though std::vector avoids this entirely.
Syntax
cpp
data_type array_name[size] = {value1, value2};

int length = sizeof(array_name) / sizeof(array_name[0]);

Fixed Size at Declaration

A C-style array's size is fixed at the moment it's declared, specified as a number inside square brackets, and unlike std::vector, it cannot grow or shrink after that.

Example: Fixed Size at Declaration

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[5] = {1, 2, 3, 4, 5};
	// Print to the console with cout
	std::cout << "Fixed size: 5" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Finding an Array's Size with sizeof

The sizeof operator applied to an array returns the total number of bytes it occupies, and dividing that by the size of a single element, using sizeof(arr[0]), is the standard way to compute how many elements it holds.

Example: Finding an Array's Size with sizeof

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[] = {1, 2, 3, 4, 5};
	// Declare count and set it to sizeof(arr) / sizeof(arr[0])
	int count = sizeof(arr) / sizeof(arr[0]);
	// Print to the console with cout
	std::cout << count << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Size of Arrays Passed to Functions

When an array is passed as a function parameter, it decays into a pointer, so sizeof inside that function reports the size of the pointer rather than the original array, requiring the element count to be passed in separately.

Example: Size of Arrays Passed to Functions

cpp
#include <iostream>

void checkSize(int arr[]) {
	std::cout << sizeof(arr) << std::endl; // reports pointer size, not array size
}

int main() {
	int arr[] = {1, 2, 3, 4, 5};
	std::cout << sizeof(arr) << std::endl; // full array size
	checkSize(arr);
	return 0;
}

std::size() for Modern Array Sizing

C++17 introduced the free function std::size(), which returns an array's element count directly without the sizeof division trick, working on both C-style arrays and STL containers.

Example: std::size() for Modern Array Sizing

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int arr[] = {1, 2, 3, 4, 5};
	// Print to the console with cout
	std::cout << std::size(arr) << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

std::vector as a Sizeable Alternative

Unlike a C-style array, a std::vector tracks its own size internally and can grow or shrink at runtime, and its .size() method always returns the current element count directly.

Example: std::vector as a Sizeable Alternative

cpp
// Include std::cout and std::cin
#include <iostream>
// Include std::vector
#include <vector>

// Program execution starts in main()
int main() {
	// Create a vector named vec (a resizable array)
	std::vector<int> vec = {1, 2, 3};
	vec.push_back(4);
	// Print to the console with cout
	std::cout << vec.size() << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Using sizeof(arr) inside a function where the array has decayed to a pointer, giving 8 instead of the length.
  2. Dividing sizeof(arr) by the wrong element type.
  3. Using a non-constant size like int a[n]; and expecting it to be standard C++ (it is a compiler extension).

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.