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

C++ Omitting Array Size

When an array is initialized with values at declaration, the size in the square brackets can be omitted entirely, and the compiler infers it automatically from the number of initializer values.
Syntax
cpp
data_type array_name[] = {value1, value2, value3};  // size is inferred

Omitting the Size at Initialization

When declaring an array and providing its initial values at the same time, the size inside the square brackets can be left empty, and the compiler counts the initializer values to determine the array's size.

Example: Omitting the Size at Initialization

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 << "Size inferred as 5 elements" << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Why This Works

The compiler can only infer an array's size when a complete list of initial values is provided at the moment of declaration, since it counts exactly how many values appear inside the braces.

Example: Why This Works

cpp
#include <iostream>

int main() {
	int arr[] = {10, 20, 30}; // compiler counts 3 initializers
	std::cout << sizeof(arr) / sizeof(arr[0]) << std::endl;
	return 0;
}

When Size Cannot Be Omitted

The size cannot be omitted if the array is declared without any initializer values at all, since the compiler would then have nothing to count and needs an explicit size to reserve memory.

Example: When Size Cannot Be Omitted

cpp
#include <iostream>

int main() {
	int arr[5]; // no initializer -- size must be given explicitly
	arr[0] = 1;
	std::cout << arr[0] << std::endl;
	return 0;
}

Omitted Size with String Arrays

Omitting the size also works naturally when initializing an array of C-style strings, letting the compiler count how many string literals were provided.

Example: Omitted Size with String Arrays

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

// Program execution starts in main()
int main() {
	std::string names[] = {"Alice", "Bob", "Carol"};
	// Loop: repeat while the condition holds
	for (int i = 0; i < 3; i++) {
		// Print to the console with cout
		std::cout << names[i] << " ";
	}
	// Print to the console with cout
	std::cout << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Combining Omitted Size with a Partial Fill

Providing fewer initializer values than intended still infers a size matching only what was actually listed, so a partially intended array should state its size explicitly instead of relying on omission.

Example: Combining Omitted Size with a Partial Fill

cpp
#include <iostream>

int main() {
	int arr[] = {1, 2}; // size inferred as 2, not larger
	std::cout << sizeof(arr) / sizeof(arr[0]) << std::endl;
	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. Omitting both the size and the initializer, like int a[];, which is an error.
  2. Assuming the compiler picks a size you did not give, when it is only counted from the initializer list.
  3. Later trying to grow such an array, when its size is fixed.

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.