How do I define a structure in C?
What is Structure: 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 member can be accessed only through the structure variable.
- Structure variables accessing the same structure but the memory allocated for each variable will be different.
Syntax of Structure:
- Struct Structure_name
- {
- mebmer_variable1;
- mebmer_variable2;
- mebmer_variable3;
- .
- .
- .
- .
- member_variable n;
- }[structure variables];
Let's see a simple example:
- #include<stdio.h>
- Struct students
- {
- char name[50]; // Structure members declaration.
- int age;
- }S1; // declaring structure variable of student.
- int main()
- {
- printf("Enter the name of the student:\n");
- scanf("%s",S1.name);
- printf("Enter the age of the student:\n");
- scanf("%d",S1.age);
- printf("Name and age of the student: %s,%d,S1.name,S1.age");
- return 0;
- }
The output of the following code is:
Enter the name of the student: Shubham
Enter the age of the student: 23
Name and age of the student: Shubham,23
Comments
Post a Comment