liveBook · Manning

Initialization

As explained in Modern C, Third Edition

Initialization is the process of providing an initial value for a C object as part of its declaration or creation.

Initialization provides an initial value for a C object. An initializer augments a declaration; when a declaration includes one, it also defines the object and requires storage for its value. section 2.3

Designated array initialization

A designated initializer assigns values to selected array positions: A[0] becomes 9.0, A[1] becomes 2.9, A[3] becomes .00007, and A[4] becomes 3.E+25. The omitted position A[2] is initialized to 0.0. Array indices begin at 0, so an array of n elements ends at index n-1. section 2.3

Choose initialization according to the object's storage duration and allocation method:

  • Rely on default initialization for objects with static or thread storage duration: arithmetic members become 0 and pointer members become nullptr. section 13.4
  • Initialize automatic and allocated objects explicitly so they enter a well-defined state before use. section 13.4
  • Use calloc for dynamically allocated objects when an all-bits-zero representation is valid; otherwise initialize through assignment or a dedicated function. section 13.4
  • Provide a consistent initialization function for each data type, including constructors such as rat_init, rat_new, rat_vinit, and rat_vnew. section 13.4
double A[5] = {                                      
   [0] = 9.0,                                         
   [1] = 2.9,
   [4] = 3.E+25,                                      
   [3] = .00007,                                      
 };

Inspect the complete designated initializer and its ordering of array elements. section 2.3

How Modern C, Third Edition covers it

How deep it goes
Descends from declaration syntax to storage-duration rules and initialization APIs, while deferring deeper object representations.
Examples
Uses scalar initialization, designated array elements, omitted zero values, and rat-specific initialization functions.
What it assumes
Assumes basic C declarations, identifiers, arrays, pointers, storage duration, and dynamic allocation terminology.
What you can do after
Apply initialization syntax and storage-duration rules, then design consistent initialization functions for allocated data types.
How it connects
Links declarations, definitions, objects, arrays, storage duration, dynamic allocation, assignment, and initialization functions.

Also explained in

Compare how 5 books explain initialization →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C, Third Edition. The section links point to the parts of the book it draws on.