Declaration and Initialization

What Is an Initializer?

An initializer gives a variable its starting value. Learn the = and brace forms in C++, what {} does, and how narrowing rules affect each one.

On this page
  1. Short answer
  2. Where initializers appear
  3. The forms in C++
  4. Empty braces and default values
  5. Narrowing with braces
  6. Initializers for structs and classes
  7. Pointers and nullptr
  8. Common mistakes
  9. Summary

Short answer

An initializer is the value, or braced list of values, that gives an object its starting value when it is created. In int x = 42;, the initializer is the 42 after the equal sign.

Where initializers appear

int count = 0;                   // a single value
int nums[3] = {1, 2, 3};         // a braced list for an array

struct Point {
    int x;
    int y;
};

Point p = {1, 2};                // a braced list for a struct

Classes work differently. Their initializer usually calls a constructor, which is covered later in this article.

The forms in C++

int a = 1;     // equal sign (copy-style)
int b(2);      // parentheses
int c{3};      // braces (C++11 and later)
int d{};       // empty braces: value-initialized, so d is 0

Each form gives the object the starting value you write. The brace form has an extra rule about narrowing, which is covered below.

C has fewer forms. It has no parentheses form for a variable, and braces work only in the = { ... } form:

int a = 1;      /* valid C */
int c = {3};    /* valid C: one value in braces */

Empty braces and default values

int x;      // local variable: the value is indeterminate
int y{};    // zero, because empty braces value-initialize the variable

A local int with no initializer has no defined value, so reading it is a bug. Empty braces give it zero. Global and static variables start at zero even without braces.

Narrowing with braces

int ok = 3.7;      // compiles; the value is cut down to 3, and compilers may warn
int bad{3.7};      // error: narrowing conversion

Braces refuse conversions that could lose information. That rule is called narrowing. The equal-sign form allows the conversion silently, so the brace form is the stricter choice.

Initializers for structs and classes

An aggregate such as a plain struct takes a list of member values:

struct Point { int x; int y; };

Point p{1, 2};     // x is 1, y is 2
Point q{};         // x and y are both 0

C++20 also allows naming members in the list, as in Point r{.x = 5};. The rules for that form are in C designated initializers.

A class that defines a constructor takes its initializer as arguments to that constructor:

#include <string>

std::string greeting("hello");   // calls a std::string constructor
std::string other{"hi"};         // so does this

How a class constructor runs during initialization is covered in c class constructor.

Pointers and nullptr

A pointer is often started at nullptr, which means it points at nothing yet:

int* p = nullptr;   // points at nothing yet

The difference between nullptr and NULL is covered in c null vs nullptr.

Common mistakes

  • The vexing parse. Widget w(); looks like an object created with no arguments. C++ reads it as a declaration of a function named w that returns a Widget. Write Widget w{}; instead to create an object.
  • Reading an uninitialized variable. int total; total += 5; reads an indeterminate value first, which is undefined behavior.
  • Narrowing surprises. Braces catch conversions that lose information. When the compiler reports narrowing, fix the type or the value instead of working around the check.
  • Mixing up initialization and assignment. int n = 5; initializes n. n = 5; assigns to an existing n. The two look alike, but they are different steps.

Summary

  • An initializer gives an object its starting value when it is created.
  • C++ has the equal-sign, parentheses, and brace forms. The brace form rejects narrowing, and empty braces give zero.
  • A class initializer calls a constructor, and C++20 lets brace lists name struct members.

The complete guide to declaration and initialization

Designated Initializers in C++
Read the guide

Keep exploring

Guides from other topics

All topics →