1. Intro
Array initialization means giving an array its initial values at the moment it is declared, using an initializer list enclosed in curly braces. C++ offers a few flexible variations on this syntax, and understanding them helps you avoid bugs caused by uninitialized or unexpectedly zero-filled elements.
Cricket analogy: Naming a playing XI before the toss and locking in the batting order avoids last-minute scrambling, just as declaring an array with an initializer list {...} sets every slot's value up front instead of leaving it to chance.
2. Syntax
// Full initialization
int a[5] = {1, 2, 3, 4, 5};
// Partial initialization (rest become 0)
int b[5] = {1, 2};
// Size omitted, inferred from initializer list
int c[] = {1, 2, 3};3. Explanation
When you write int a[5] = {1, 2, 3, 4, 5};, each value in the braces maps to the corresponding index, so a[0] is 1 and a[4] is 5. If you provide fewer values than the declared size, as in int b[5] = {1, 2};, C++ performs partial initialization: b[0] and b[1] get the given values, and every remaining element (b[2] through b[4]) is automatically zero-filled — this is guaranteed behavior, not garbage memory. You can also omit the size entirely, as in int c[] = {1, 2, 3};, and the compiler infers the array's size (3, here) directly from the number of elements in the initializer list. Note that an uninitialized local array (no initializer list at all) contains indeterminate garbage values, so it is good practice to always initialize arrays explicitly.
Cricket analogy: Filling a 5-slot batting order card with exactly 5 names maps directly like int a[5]={1,2,3,4,5}, but if you only write in the first 2 batsmen and leave 3 slots blank, a well-run scoring system defaults the rest to 'not out 0' rather than showing garbage, just as C++ zero-fills b[2] through b[4].
4. Example
#include <iostream>
using namespace std;
int main() {
int a[5] = {1, 2, 3, 4, 5};
int b[5] = {1, 2}; // rest zero-filled
int c[] = {10, 20, 30}; // size inferred as 3
cout << "a: ";
for (int i = 0; i < 5; i++) cout << a[i] << " ";
cout << endl;
cout << "b: ";
for (int i = 0; i < 5; i++) cout << b[i] << " ";
cout << endl;
cout << "c size: " << (sizeof(c) / sizeof(c[0])) << endl;
return 0;
}5. Output
a: 1 2 3 4 5
b: 1 2 0 0 0
c size: 36. Key Takeaways
- An initializer list
{...}at declaration sets an array's starting values in order. - Partial initialization is legal — any elements not given a value are automatically set to
0. - The array size can be omitted (
int c[] = {...}) and the compiler infers it from the initializer list. - An uninitialized local array holds indeterminate garbage values, so explicit initialization is best practice.
Practice what you learned
1. What is the value of `b[3]` after `int b[5] = {1, 2};`?
2. What size is inferred for `int c[] = {10, 20, 30};`?
3. What value do the elements of `int x[4];` (declared with no initializer) hold?
4. Is `int a[5] = {1, 2, 3, 4, 5, 6};` valid C++?
5. Which declaration lets the compiler determine the array size automatically?
Was this page helpful?
You May Also Like
Arrays in C++
Learn what an array is in C++, how to declare and access a fixed-size collection of same-type elements, and why zero-based indexing matters.
Multidimensional Arrays in C++
Understand multidimensional arrays in C++ as arrays of arrays, how 2D arrays model rows and columns, and how row-major storage works.
Variables in C++
Learn what are variables in C++, types of variables, declaration, initialization and examples with programs.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics