Incomputer programming,initialization orinitialisation is theassignment of an initial value for adata object or variable. The manner in which initialization is performed depends on theprogramming language, as well as the type, storage class, etc., of an object to be initialized. Programming constructs which perform initialization are typically calledinitializers andinitializer lists. Initialization is distinct from (and preceded by)declaration, although the two can sometimes be conflated in practice. The complement of initialization isfinalization, which is primarily used for objects, but not variables.
Initialization is done either by statically embedding the value at compile time, or else by assignment atrun time. A section of code that performs such initialization is generally known as "initialization code" and may include other, one-time-only, functions such as opening files; inobject-oriented programming, initialization code may be part of aconstructor (class method) or aninitializer (instance method). Setting a memory location tohexadecimal zeroes is also sometimes known as "clearing" and is often performed by anexclusive or instruction (both operands specifying the same variable), atmachine code level, since it requires no additional memory access.
In C/C99/C++, aninitializer is an optional part of adeclarator. It consists of the '=' character followed by anexpression or a comma-separated list of expressions placed in curly brackets (braces). The latter list is sometimes called the "initializer list" or "initialization list" (although the term "initializer list" is formally reserved for initialization of class/struct members in C++;see below).A declaration which creates a data object, instead of merely describing its existence, is commonly called adefinition.
Many find it convenient to draw a distinction between the terms "declaration" and "definition", as in the commonly seen phrase "the distinction between adeclaration anddefinition...", implying that a declaration merely designates a data object (or function). In fact, according to theC++ standard, a definitionis a declaration. Still, the usage "declarations and definitions", although formally incorrect, is common.[1] Although all definitions are declarations, not all declarations are definitions.
C examples:
inti=0;intk[4]={0,1};chartx[3]='a';charty[2]='f';structPoint{intx;inty;}p={.y=13,.x=7};
C++ examples:
inti2(0);intj[2]={rand(),k[0]};MyClass*xox=newMyClass(0,"zaza");pointq={0,i+1};
In C++, aconstructor of a class/struct can have aninitializer list within the definition but prior to the constructor body. It is important to note that when you use an initialization list, the values are not assigned to the variable. They are initialized. In the below example, 0 is initialized into re and im.Example:
structIntComplex{IntComplex():re(0),im(0){}intre;intim;};
Here, the construct : re(0), im(0)
is the initializer list.
Sometimes the term "initializer list" is also used to refer to the list of expressions in the array or struct initializer.
C++11 provides for amore powerful concept of initializer lists, by means of a template, calledstd::initializer_list.
Data initialization may occur without explicit syntax in a program to do so. For example, ifstatic variables are declared without an initializer, then those ofprimitive data types are initialized with the value of zero of the corresponding type, while static objects of class type are initialized with theirdefault constructors.