Structure in C language
Structure :- Structure is a collection of same or different types of data elements in a single entity. It is a combination of primitive and derived datatype variable. It is used to create a user define datatypes. Size of the structure is sum of all the member variable(structure element) size. Structure elements can be accessed through a structure variable using a dot operator. Structure elements can be accessed through a pointer to a structure using the arrow ( -> ) operator. struct keyword is used to define a structure. The general syntax of structure definition is-struct <structure_tag>
{
structure element ;
structure element 2;
.................................
.................................
structure element n;
} ;
Structure variable can be declared in following two ways.
a) Declaring structure variables using structure tag name
Example :
struct student
{
char name[10];
int roll;
char clas[10];
};
struct student s1,s2;
Once the structure is define structure variable can be created many time in the program. Structure variable is consume the space in memory.It is important to note that definition of structure template does not consume any space in memory for the members
b) Declaring structure variables with the structure definition
Example
struct student
{
char name[10];
int roll;
char clas[5];
} s1,s2,s3;
Example :- Define the student structure input student information and display
#include<stdio.h> #include<conio.h> struct student { char name[20]; int roll_no; char clas[10]; char dob[10]; //Date of birth }; int main() { struct student s1; printf("Enter name of student\t:-"); gets(s1.name); printf("Roll no.\t:-"); scanf("%d",&s1.roll_no); printf("Class\t:-"); scanf("%s",s1.clas); printf("Date of birth\t:-"); scanf("%s",s1.dob); printf("\nEntered student information\n"); printf("\nName\t\t:-%s\n",s1.name); printf("Roll No.\t\t:-%d\n",s1.roll_no); printf("Class\t\t:-%s\n",s1.clas); printf("Date of birth\t:-%s\n\n",s1.dob); getch(); }
Output:-
Next :- Initialization of structure variable and assign
0 Comments