Structures and Unions
Structures and unions! These are fundamental data types in C that help in managing and organizing data efficiently.
Structures (Structs)
#include <stdio.h>
// Define a structure
struct Person {
char name[50];
int age;
float height;
};
int main() {
// Declare and initialize a structure variable
struct Person person1 = {"Alice", 30, 5.5};
printf("Name: %s\n", person1.name); // Outputs: Alice
printf("Age: %d\n", person1.age); // Outputs: 30
printf("Height: %.1f\n", person1.height); // Outputs: 5.5
return 0;
}Unions
Comparison: Structures vs. Unions
Using Structures and Unions
Summary
Last updated