Initialization Rules for Variables in C++ (Built-in and Class Types)

When a variable is defined without an initializer (e.g., int i;), the system may implicitly initialize it. Whether the system performs this implicit initialization and the value it assigns depend on both the type of the variable and where it is defined.

1. Initialization of Built-in Type Variables

Weather a built-in variable is automatically initialized depends on its scope.

Global Scope

Built-in type variables defined in the global scope are automatically initialized to zero by the compiler.

#include <iostream>
using namespace std;

// Global built-in variables
int globalInt;          // Automatically initialized to 0
float globalFloat;      // Automatically initialized to 0.0
double globalDouble;    // Automatically initialized to 0.0
char globalChar;        // Automatically initialized to '\0'

int main(int argc, char **argv) {
    return EXIT_SUCCESS;
}

Local Scope (Inside a Function)

Built-in type variables defined inside a functon body have indeterminate values (some compilers might initialize them to zero, but relying on this leads to undefined behavior).

#include <iostream>
using namespace std;

int main(int argc, char **argv) {
    // Local built-in variables
    int localInt;          // Not initialized; value is random (may be 0 depending on compiler)
    float localFloat;      // Not initialized; value is random
    double localDouble;    // Not initialized; value is random
    char localChar;        // Not initialized; value is random

    return EXIT_SUCCESS;
}

Built-in Type Arrays

The same initialization rules apply to arrays of built-in types.

#include <iostream>
using namespace std;

// Global array of built-in type
int globalArray[5];  // All 5 elements are initialized to 0

int main(int argc, char **argv) {
    // Local array of built-in type
    int localArray[5];  // Not initialized; elements have indeterminate values
    return EXIT_SUCCESS;
}

2. Initialization of Class Type Variables

Class type variables are initialized through constructors. Regardless of whether the object is defined globally or locally, its constructor (either the default constructor or a specified parameterized constructor) is always called. To understand how constructors initialize class data members, it is essential to know when initialization occurs.

Note: Initialization in a constructor happens in the constructor initializer list, not inside the constructor body.

class Foo {
public:
    Foo(int i) : _integer(i) {   // Initializer list performs initialization
        // Constructor body
        // Assignment happens here, not initialization
        _integer = i;
    }
private:
    int _integer;
};

Since constructor initialization occurs in the initializer list, for built-in type members that are not explicitly initialized in the list, the same rules as for built-in variables apply. Consider the following example:

#include <iostream>
using namespace std;

class Foo {
public:
    Foo(void) : _explicitInit(1024) {}
private:
    int _explicitInit;   // Explicitly initialized in the initializer list
    int _implicitInit;   // Not explicitly initialized in the constructor
};

Foo globalFoo;  // Global class object:
                // _explicitInit is explicitly initialized to 1024
                // _implicitInit is automatically initialized to 0

int main(int argc, char **argv) {
    Foo localFoo;       // Local class object:
                        // _explicitInit is explicitly initialized to 1024
                        // _implicitInit is NOT automatically initialized; value is random
    return EXIT_SUCCESS;
}

Original article reference: https://blog.csdn.net/jzwong/article/details/45022591

Tags: C++ initialization built-in types class types Constructors

Posted on Mon, 11 May 2026 06:21:56 +0000 by ifis