Vector Initialization

Are ints Initialized to 0 in C?

Not always. Global and static ints start at 0 in C and C++, but local ints can hold an unknown value. Learn how to tell them apart and set zero on purpose.

On this page
  1. No, not always
  2. Globals and statics start at 0
  3. Local ints may hold an unknown value
  4. Set zero on purpose
  5. Heap-allocated ints
  6. Vector elements are different
  7. Catch unset values early
  8. Summary

No, not always

Whether an int starts at 0 depends on where it is declared. In C and C++, variables with static storage duration are set to 0 before your code runs. That group includes global variables and variables marked static. A plain local int inside a function is not in that group. Its value is unknown until your code assigns one.

Globals and statics start at 0

#include <iostream>

int global_count;        // 0

int main() {
    static int calls;    // 0
    std::cout << global_count << ' ' << calls << '\n';  // 0 0
}

The output is 0 0.

Local ints may hold an unknown value

int main() {
    int total;                 // no value assigned yet
    // std::cout << total;     // undefined behavior: total was never set
    total = 5;                 // fine once a value is assigned
}

The language does not promise any particular value for total before it is set. Reading it is undefined behavior, so the program might print a leftover value from memory or act in some other unexpected way. The result can change from one run to the next, or from one compiler to another.

Where a variable is stored decides which rule applies. For the stack and heap side of that, see c heap vs stack.

Set zero on purpose

Give the variable an initializer:

int a{};     // 0
int b = 0;   // 0
int c{42};   // 42

Arrays follow the same idea. An empty braced list sets every element to 0, and a partial list sets the rest to 0:

int scores[4] = {};     // 0 0 0 0
int levels[4] = {1};    // 1 0 0 0

Heap-allocated ints

Memory from new follows the same split:

int* p = new int;     // value is unknown
int* q = new int();   // 0

delete p;
delete q;

Vector elements are different

Vector elements get a value when the vector creates them, even if you do not set one:

#include <vector>

std::vector<int> v(5);   // five elements, all 0

Vectors follow their own rules for setting values. For the different ways to create one, see c vector initialization.

Catch unset values early

  • Turn on compiler warnings such as -Wall -Wextra in GCC or Clang. They flag some uses of unset variables, but not all of them.
  • Valgrind’s Memcheck tool reports when an uninitialized value affects a branch or printed output.
  • Give every local variable a value when you declare it, even if that value is 0.

Summary

Global and static ints start at 0. A plain local int does not, and reading it before it is set is undefined behavior. Use {} or = 0 when you want a known starting value, and check that your compiler warnings are turned on.

The complete guide to vector initialization

How to Initialize a Vector in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

std::array in C++

A complete guide to std::array in C++: declaring, initializing, reading elements, looping, passing to functions, and how it compares to C-style arrays.

std::array 5 min read