Introduction to Structures in C Programming
Structures in C Programming
Introduction to Structures in C Programming
Structures in C programming are used to store multiple types of data in a single variable. Unlike arrays, structures can store different data types such as int, float, and char together.
Structures in C programming are useful for representing real-world entities like students, employees, and records.
What is a Structure in C Programming
A structure is a user-defined data type that groups related variables of different data types.
Syntax of Structure
data_type member1;
data_type member2;
data_type member3;
};
Example
int id;
char name[20];
float marks;
};
Declaring Structure Variables
After defining a structure, you can create variables.
Initializing Structure
Accessing Structure Members
Structure members are accessed using the dot (.) operator.
struct Student {
int id;
char name[20];
float marks;
};
int main() {
struct Student s1 = {1, “Amit”, 90.5};
printf(“ID: %d\n”, s1.id);
printf(“Name: %s\n”, s1.name);
printf(“Marks: %f”, s1.marks);
return 0;
}
Structure with User Input
struct Student {
int id;
char name[20];
};
int main() {
struct Student s1;
printf(“Enter ID: “);
scanf(“%d”, &s1.id);
printf(“Enter Name: “);
scanf(“%s”, s1.name);
printf(“ID: %d\n”, s1.id);
printf(“Name: %s”, s1.name);
return 0;
}
Array of Structures
struct Student {
int id;
char name[20];
};
int main() {
struct Student s[2];
for(int i = 0; i < 2; i++) {
printf(“Enter ID: “);
scanf(“%d”, &s[i].id);
printf(“Enter Name: “);
scanf(“%s”, s[i].name);
}
for(int i = 0; i < 2; i++) {
printf(“%d %s\n”, s[i].id, s[i].name);
}
return 0;
}
Why Structures are Important
Key Benefits
- Store different data types together
- Represent real-world data
- Improve code organization
- Useful in large programs
Difference Between Array and Structure
Array
- Same data type
- Indexed access
Structure
- Different data types
- Access using member names
Start Learning C Programming
Practice structures in C programming to handle complex data efficiently.
Summary
Structures in C programming allow grouping of different data types into one unit. They are useful for managing complex data.
FAQs
What is a structure in C programming?
A structure is a user-defined data type that stores different data types together.
How to access structure members?
Using dot operator.
Can structure store different data types?
Yes.
What is array of structures?
A collection of structure variables.



