Structure is other data type of c programming.structure is an group of many datatypes.it is used widely in c programming.structures are used to make program simpler and easy to understand.structure allows us to store many variables of different datatypes togather. For example:we want to store information about any person then we have to declare some variables which are: age,e-mail and name of that person.then we have two ways
First method:
char name[100];
int age;
char e_mail[100];
it is easy for one type (person) but if we have many togather then it is very confusing.Let us see synatx for structure and then we will solve this problem by structure:
Syntax:
struct <structure_name>
{
element no.1;
element no.2;
element no.3;
element no.4;
.
.
.
.
element no.n;
char e_mail[100];
First method:
char name[100];
int age;
char e_mail[100];
it is easy for one type (person) but if we have many togather then it is very confusing.Let us see synatx for structure and then we will solve this problem by structure:
Syntax:
struct <structure_name>
{
element no.1;
element no.2;
element no.3;
element no.4;
.
.
.
.
element no.n;
}
METHOD 2
struct person
{
char name[100];
int age;char e_mail[100];
}
it is very easy then the First method.Let us solve this problem by structure.
SOLUTION:
#include<stdio.h>
#include<conio.h>
struct person
{
char name[100];
int age;
char e_mail[100];
}
main()
{
struct person p;
clrscr();
printf("Enter your name: ");
fflush(stdin);
gets(p.name);
printf("\nEnter your e-mail: ");
fflush(stdin);
gets(p.e_mail);
printf("\nEnter your age: ");
scanf("%d",&p.age);
printf("\n\nYour name is %s",p.name);
printf("\n\nyour e-mail is %s",p.e_mail);
printf("\n\nYour age is %d",p.age);
getch();
}