100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Array Initialization in C++

Learn the different ways to initialize a C++ array at declaration, including partial initialization, zero-filling, and omitting the size.

ArraysBeginner6 min readJul 7, 2026
Analogies

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

cpp
// 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

cpp
#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

text
a: 1 2 3 4 5 
b: 1 2 0 0 0 
c size: 3

6. 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

Was this page helpful?

Topics covered

#CStudyNotes#Programming#ArrayInitializationInC#Array#Initialization#Syntax#Explanation#DataStructures#StudyNotes#SkillVeris