What is the structure in C Language?
- The structure is a user-defined data type that allows storing multiple types of data in a single unit. It occupies the sum of the memory of all members.
- The structure members can be accessed only through structure variables.
- Structure variables accessing the same structure but the memory allocated for each variable will be different.
Syntax of structure
- struct structure_name
- {
- Member_variable1;
- Member_variable2
- .
- .
- }[structure variables];
Let’s see a simple example.
- #include <stdio.h>
- struct student
- {
- char name[10]; // structure members declaration.
- int age;
- }s1; //structure variable
- int main()
- {
- printf(“Enter the name”);
- scanf(“%s”,s1.name);
- printf(“\n”);
- printf(“Enter the age”);
- scanf(“%d”,&s1.age);
- printf(“\n”);
- printf(“Name and age of a student: %s,%d”,s1.name,s1.age);
- return 0;
- }
Output:
Enter the name shikha Enter the age 26 Name and age of a student: shikha,26
Recent Posts