Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

Array initialization in C is the process of providing initial values to an array’s contiguous memory locations at the point of declaration. This is accomplished using either a brace-enclosed, comma-separated list of initializers or, for character arrays, a string literal. For arrays with static or thread storage duration, these initializers must be compile-time constant expressions. For arrays with automatic storage duration (since C99), the initializer list may contain non-constant expressions, such as variables or function return values.

Complete and Partial Initialization

When an array is declared with an explicit size, an initializer list can be provided. If the list contains exactly the same number of elements as the array size, it is a complete initialization. If the list contains fewer elements, it is a partial initialization, and the C compiler automatically zero-initializes all remaining elements.
int complete[5] = {10, 20, 30, 40, 50};

// partial[0]=10, partial[1]=20, partial[2]=0, partial[3]=0, partial[4]=0
int partial[5] = {10, 20}; 

// Pre-C23 idiomatic approach to zero-initialize an entire array
int all_zeros[100] = {0}; 

// C23 approach to zero-initialize an entire array
int empty_zeros[100] = {};

// Automatic storage duration array using non-constant expressions (C99+)
int x = 5;
int dynamic_init[2] = {x, x + 1};

Implicit Sizing

If the array size is omitted from the square brackets [] during declaration, the compiler infers the array’s length from the initializer list. The inferred length is determined by the highest initialized index plus one, accounting for both sequentially listed elements and explicitly designated indices.
// The compiler allocates memory for exactly 4 integers (indices 0 through 3)
int implicit_sequential[] = {5, 10, 15, 20}; 

// The compiler allocates memory for 10 integers (highest index is 9)
// implicit_designated[9] = 42, all other elements are zero-initialized
int implicit_designated[] = { [9] = 42 };

Designated Initializers (C99 Standard)

C99 introduced designated initializers, allowing the explicit initialization of specific indices using the [index] = value syntax. Any index not explicitly designated is zero-initialized. Subsequent values without a designator will initialize the next sequential index.
// designated[0]=0, designated[1]=0, designated[2]=88, designated[3]=0, designated[4]=99
int designated[5] = {[2] = 88, [4] = 99};

// mixed[0]=0, mixed[1]=10, mixed[2]=20, mixed[3]=30, mixed[4]=0
int mixed[5] = {[1] = 10, 20, 30}; 

Character Array (String) Initialization

Character arrays can be initialized using standard brace-enclosed character constants or shorthand string literals. When using a string literal, the compiler implicitly appends a null-terminator (\0) to the array, provided there is sufficient space.
// Explicit character initialization
char chars[] = {'a', 'b', 'c', '\0'};

// String literal initialization (size is 4 due to implicit '\0')
char str1[] = "abc";

// Explicit size with string literal (remaining elements zero-initialized)
char str2[10] = "abc";

// Edge case: Size exactly matches string length. No null-terminator is appended.
char str3[3] = "abc"; 

Multidimensional Array Initialization

Multidimensional arrays are initialized using nested brace-enclosed lists. Because C stores multidimensional arrays in row-major order (contiguous memory), the inner braces are syntactically optional but highly recommended for structural clarity.
// Explicitly nested initialization
int matrix_nested[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};

// Flat initialization (relies on row-major memory layout)
int matrix_flat[2][3] = {1, 2, 3, 4, 5, 6};

// Partial multidimensional initialization
// Row 0: {1, 2, 0}, Row 1: {4, 0, 0}
int matrix_partial[2][3] = {
    {1, 2},
    {4}
};

Strict Constraints

  1. Excess Elements: Providing more initializers than the declared size of the array results in a compiler error.
  2. Variable Length Arrays (VLAs): Arrays declared with a non-constant size (introduced in C99) cannot be initialized at the point of declaration.
  3. Empty Initializer Lists: Prior to the C23 standard, empty initializer lists were strictly prohibited and required at least one expression (e.g., {0}). As of C23 (ISO/IEC 9899:2024), empty braces ({}) are officially permitted and guarantee zero-initialization of the entire array.
Master C with Deep Grasping Methodology!Learn More