Introduction to C Language Initialization Methods
– Overview of Aggregate Initialization
Author: Luo Guangxuan
The {} expression is a standard and core initialization method in C language, known as Aggregate Initialization or List Initialization, applicable to most data types (variables, arrays, structures, unions, etc.). However, it is important to note that different C standards (C89/C99/C11) have varying support for {} initialization, although the core usage is compatible across all standards.
1. Core Scenarios: Basic Usage of {} Initialization (Supported in C89 and above)
These are the most commonly used and compatible scenarios, which you have frequently encountered in your previous code (such as array initialization).
1. Initialization of Ordinary Variables (Direct Assignment List)
Single variables can be assigned values using {}, with concise syntax equivalent to direct assignment using =.
// Equivalent to int x = 10;
int x = {10};
// Equivalent to float y = 3.14f;
float y = {3.14};
// Character type is also supported
char c = {‘A’};
2. Array Initialization (Core Scenario, Reflecting Your Previous Code)
{} is the standard way to initialize arrays, supporting one-dimensional, two-dimensional, and even multi-dimensional arrays, and can omit some elements (automatically filling with 0).
// One-dimensional array: Fully initialized (number of elements = length of initialization list)
int arr1[] = {1, 2, 3, 4};
// Compiler automatically infers length = 4, equivalent to int arr1[4] = {1,2,3,4}
// One-dimensional array: Partially initialized (unspecified elements automatically filled with 0)
int arr2[5] = {1, 2};
// Result: [1, 2, 0, 0, 0]
// Two-dimensional array: Consistent with your previous code
int arr3[2][3] = {{1, 2, 3}, {4, 5, 6}};
// Complete initialization
int arr4[3][2] = {{1}, {2}, {3}};
// Partial initialization: [1,0], [2,0], [3,0]
// Pointer array: The ptr_arr you constructed in your previous code is this usage
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int* ptr_arr[] = {a[0], a[1]};
// Pointer array, each element points to a row of the two-dimensional array
3. Structure/Union Initialization
{} can initialize structures/unions in member order, which is the standard writing method in C language.
struct Person {
char name[20];
int age;
float score;
};
// Initialize in the order of structure members
struct Person p1 = {“Zhang San”, 20, 95.5f};
// Partial initialization: unspecified members automatically filled with 0 (strings filled with ‘\0’)
struct Person p2 = {“Li Si”, 19};
// score defaults to 0.0f
2. Extended Scenarios: New {} Initialization Features Added in C99 and Above
C99 and subsequent standards (C11/C17) have expanded the functionality of {} initialization, making it more flexible but requiring attention to compiler compatibility (mainstream GCC and Clang support it, older MSVC versions need to enable C99 mode).
1. Designated Initialization (Initialization by Index/Member Name)
Initialization can be done directly by specifying array indices or structure member names without following the order, with unspecified elements still filled with 0.
// Array designated initialization (C99+): only initialize elements at index 0 and 3
int arr5[5] = {[0] = 10, [3] = 20};
// Result: [10, 0, 0, 20, 0]
// Structure designated initialization (C99+): initialize by member name, order does not matter
struct Person p3 = {.age = 22, .name = “Wang Wu”, .score = 98.0f};
2. Omitted Array Length + Partial Initialization
C99 allows the omission of array length, and combined with designated initialization, the compiler automatically infers the maximum length of the array.
// Maximum index is 4, compiler infers array length = 5
int arr6[] = {[4] = 5};
// Result: [0, 0, 0, 0, 5]
3. Compound Literals (Temporary Object Initialization)
Using (type){} generates a temporary “compound literal”, which can be directly assigned to a pointer or used as a function parameter (C99+).
// Temporary array literal, assigned to pointer
int* p = (int[]{1, 2, 3, 4};
printf(“%d”, p[2]);
// Output 3
// Temporary structure literal, used as function parameter
void print_person(struct Person p) {
printf(“%s %d\n”, p.name, p.age);
}
print_person((struct Person){“Zhao Liu”, 21});
// Directly passing temporary structure
4. Unified Initialization (C11+)
C11 further extends to “unified initialization”, allowing the omission of the equal sign =, making the syntax more concise (compatible with previous usage).
// Omitted =, equivalent to int x = {10};
int x = {10};
// Unified initialization of arrays
int arr7[] = {1, 2, 3};
// Unified initialization of structures
struct Person p4 = {.name = “Qian Qi”, .age = 23};
3. Key Considerations (Core Pitfalls to Avoid)
1. The number of initializers cannot exceed the array length: for example, int arr[3] = {1,2,3,4}; is incorrect, and the compiler will report an error (too many initializers).
2. C89 limitations on {} initialization: C89 does not support “designated initialization”, “compound literals”, or “unified initialization with omitted equal sign”, and the length of arrays cannot be omitted during initialization (unless there is a complete initialization list). For example:
o C89 does not support int arr[] = {[2] = 3}; (designated initialization);
o C89 supports int arr[] = {1,2,3}; (complete initialization list, length can be omitted), but does not support int arr[5] = {[0] = 1};.
3. Default initialization of global/static variables: Global variables and static variables (static) will default to 0 if partially initialized with {}; if not initialized at all, they will also default to 0 (local variables that are not initialized will have random values).
// Global array: unspecified elements default to 0
int global_arr[5] = {1};
// Static array: uninitialized, defaults to all 0
static int static_arr[3];
4. Special usage of string initialization: Strings are essentially character arrays and can be initialized using {}, but must manually add ‘\0’, or directly use string literals (which automatically fill with ‘\0’).
// Equivalent to char str[] = “hello”; (automatically fills ‘\0’)
char str1[] = {‘h’,’e’,’l’,’l’,’o’,’\0′};
// Error: no ‘\0’, not a valid string
char str2[] = {‘h’,’e’,’l’,’l’,’o’};
Conclusion
{} is the standard initialization method in C language, primarily used for initializing arrays, structures, ordinary variables, etc., with good compatibility (basic usage supported by all C standards), while extended usage (designated initialization, compound literals, etc.) requires support from C99 and above.