Structure Initialization in C
In C, structures are user-defined data types that group related variables under one name. Just like arrays, structures allow storing multiple variables of different types together. Initialization of structures refers to assigning initial values to the members of a structure at the time of declaration or afterward.
Initializing a Structure at the Time of Declaration
You can initialize a structure at the time of its declaration by using an initialization list inside curly braces. This method allows you to assign values to each structure member in the order they are declared.
1: Initializing a Structure at the Time of Declaration
#include <stdio.h>
struct Person {
char name[50];
int age;
};
// Main Function
int main() {
// Initializing structure at the time of declaration
struct Person person1 = {"John Doe", 25}; // Name is "John Doe", age is 25
// Printing initialized structure values
printf("Name: %s, Age: %d\n", person1.name, person1.age);
return 0;
}
Output
Explanation
In this example, we have declared a structure called Person
that contains two members: name
(a string) and age
(an integer). While declaring the structure variable person1
, we initialized it with values. The name is set to "John Doe" and the age is set to 25. These values are then printed to the console.
Initializing a Structure After Declaration
Structures can also be initialized after their declaration. This is done by assigning values to the structure members one by one.
2: Initializing a Structure After Declaration
#include <stdio.h>
struct Person {
char name[50];
int age;
};
// Main Function
int main() {
// Declaring a structure variable
struct Person person2;
// Initializing structure members individually
strcpy(person2.name, "Alice Smith"); // Assigning value to the name member
person2.age = 30; // Assigning value to the age member
// Printing initialized structure values
printf("Name: %s, Age: %d\n", person2.name, person2.age);
return 0;
}
Output
Explanation
In this example, we declared a structure variable person2
without initializing it initially. Later, we assigned values to the structure members individually: the name
is set to "Alice Smith" and the age
is set to 30 using the strcpy
function for the string and direct assignment for the integer. The values are printed to the console.
Default Initialization
If no values are provided for the structure members, the default initialization happens based on the type. For instance, integer members are initialized to zero, and string members to an empty string.
3: Default Initialization of a Structure
#include <stdio.h>
struct Person {
char name[50];
int age;
};
// Main Function
int main() {
// Declaring a structure variable without initializing
struct Person person3;
// Printing default values (initialized to zero)
printf("Name: %s, Age: %d\n", person3.name, person3.age); // Default values will be printed
return 0;
}
Output
Explanation
This example demonstrates what happens when a structure is declared but not explicitly initialized. The default values for the structure members are automatically assigned: the name
is an empty string and the age
is 0 (since integers are initialized to zero). These default values are then printed to the console.