Structure
Description:
1 Structures:
A structure is a user defined data type in c.A structure creates a data type that can be used to group items of possibly
different types into a single type.
Example:
To store the employee details
struct employee |struct=keyword employee=tag or group
name
{
|
char name[10];
|members or fields of structure
int id[5];
float salary;
};
1.1 Write a program using structures, to store one student information
and print it
#include<stdio.h>
struct student
{
char name[10];
int age;
int roll_no;
int marks;
char mail_id[25];
};
int main()
{
struct student a;//struct student is an data type and a is the
object that holds all information
printf(“enter the student name,age,rollnum,marks and mail_id
orderly\n”);
scanf(“%s %d %d %d
%s”,a.name,&a.age,&a.roll_no,&a.marks,a.mail_id);/*scanning the
student information */
printf(“the student information is”);
printf(“\n name=%s\n age=%d\n roll_no=%d\n marks=%d mail_id=
%s\n”,a.name,a.age,a.roll_no,a.marks
,a.mail_id);//printin
g the student information
}
1.2 Write a program using structures to store 5 student information
and print it without using array.
#include<stdio.h>
struct student
{
char name[10];
int age;
int marks;
int rollnum;
};
int main()
{
struct student a,b,c,d,e;//5 different variables for 5 different
students
printf(“enter student 1 information name,age,rollnum and marks\n”);
scanf(“%s %d %d %d”,a.name,&a.age,&a.marks,&a.rollnum);//scanning
student 1 information upto student 5 below
printf(“enter student 2 information name,age,rollnum and marks\n”);
scanf(“%s %d %d %d”,b.name,&b.age,&b.marks,&b.rollnum);
printf(“enter student 3 information name,age,rollnum and marks\n”);
scanf(“%s %d %d %d”,c.name,&c.age,&c.marks,&c.rollnum);
printf(“enter student 4 information name,age,rollnum and marks\n”);
scanf(“%s %d %d %d”,d.name,&d.age,&d.marks,&d.rollnum);
printf(“enter student 5 information name,age,rollnum and marks\n”); scanf(“%s %d %d %d”,e.name,&e.age,&e.marks,&e.rollnum);
printf(“student 1 information”);//printing the 5 students infromation
printf(“\n name=%s\n age=%d\n marks=%d\n rollnum=
%d\n”,a.name,a.age,a.marks,a.rollnum);
printf(“student 2 information”);
printf(“\n name=%s\n age=%d\n marks=%d\n rollnum=
%d\n”,b.name,b.age,b.marks,b.rollnum);
printf(“student 3 information”);
printf(“\n name=%s\n age=%d\n marks=%d\n rollnum=
%d\n”,c.name,c.age,c.marks,c.rollnum);
printf(“student 4 information”);
printf(“\n name=%s\n age=%d\n marks=%d\n rollnum=
%d\n”,d.name,d.age,d.marks,d.rollnum);
printf(“student 5 information”);
printf(“\n name=%s\n age=%d\n marks=%d\n rollnum=
%d\n”,e.name,e.age,e.marks,e.rollnum);
}
1.3 write a program using structures to store information of 5
students using array and print them.
#include<stdio.h>
struct student//structure definition
{
char name[10],mail_id[35];
int age,roll_num,marks;
};
int main()
{
struct student a[5];//declaring data type of array which stores
our structure members
int i;
for(i=0;i<5;i++)//user input of 5 student information using array
{
printf(“enter the %d student information
name,age,rollnum,emailid and marks in order\n”,i+1);
scanf(“%s %d %d %s
%d”,a[i].name,&a[i].age,&a[i].roll_num,a[i].mail_id,&a[i].marks);
}
for(i=0;i<5;i++)//printing of 5 students information using array
{
printf(“\nstudent %d details”,i+1);
printf(“\n name=%s\n age=%d\n rollno=%d\n mailid=%s\n marks=
%d\n”,a[i].name,a[i].age,a[i].roll_num,a[i].mail_id,a[i].marks);
}
}