Zero Initialization in C++

C++Server Side ProgrammingProgramming

Zero initialization is setting the initial value of an object in c++ to zero.


T{} ;
char array [n] = “”;

The situations in which zero initialization are performed are −

  • Named variable with static or thread-local storage is initialized to zero.

  • It is used as initialization of values for non-class types and members of a class that do not have a constructor.

  • It is used to initialize a character array when its length is greater than the number of characters that are to be assigned.

Points to remember

  • Some types of variables like static variables and thread-local variables are first initialized to zero then reinitialized to a value when called.

  • A zero-initialized pointer is called a null pointer.


Program to show the implementation of zero initialization in C++ −

 Live Demo

#include <iostream>
#include <string>
using namespace std;
struct zeroInitialization {
   int x, y, z;
float f[3];
int* p;
string s;
int main(int argc, char* argv[]){
   zeroInitialization obj = zeroInitialization();
   cout<<"Zero initialized object variable :\t";
   cout<<"Zero initialized float value :\t";
   cout<<"Zero initialized pointer value :\t";
   return 0;


Zero initialized object variable : 0 0 0
Zero initialized float value : 0 0 0
Zero initialized pointer value : 0
Published on 17-Apr-2020 14:46:38