You are on page 1of 4

Lab Session 10

ROMAIL BUTT (19689)

OBJECT
Study of Structures

THEORY
If we want a group of same data type we use an array. If we want a group of elements of different data
types we use structures. For Example: To store the names, prices and number of pages of a book you can
declare three variables. To store this information for more than one book three separate arrays may be
declared. Another option is to make a structure. No memory is allocated when a structure is declared. It
simply defines the “form” of the structure. When a variable is made then memory is allocated. This is
equivalent to saying that there is no memory for “int”, but when we declare an integer i.e. int var; only
then memory is allocated.
Consider the following example of a structure:
Syntax
struct personnel
{
char name[50];
int agentno;
};
void main(void)
{
struct personnel agent1={“Mustafa”,35};
printf(“%s”,agent1.name);
printf(“%d”,agent1.agentno);
getch();
}
QUESTION NO 1:
Declare a structure named student that stores the student_ id, name and MARKS.

#include<stdio.h>
struct student
{
char name[10];
int id;
float marks;
} student;
main()
{
printf("Student detail:");
printf("\n");
printf("NAME:");
scanf("%s",&student.name);
printf("\n");
printf("ID:");
scanf("%d",&student.id);
printf("\n");
printf("MARKS:");
scanf("%f",&student.marks);
printf("\n");
}
QUESTION NO 2:
Declare an array of 5 student for the structure defined in question 1. Also write statements
to assign the following values to the student. Student_ id = “Your_roll_no” name
=”your_name” and department = “CS dept”

#include<stdio.h>
#include <string.h>
#include <stdlib.h>
struct student
{
char name[10];
int student_id;
char department[10];
}student[5];
main()
{
int a;
printf("How many student do you Want:\n");
scanf("%d",&a);
for(int i=0;i<a;i++)
{
system("cls");
printf("STUDENT_%d\n",i+1);
printf("Name:");
scanf("%s",&student[i].name);
printf("\n");
printf("Student_ID:");
scanf("%d",&student[i].student_id);
printf("\n");
printf("Department:");
scanf("%s",&student[i].department);
printf("\n");

}
for(int i=0;i<a;i++)
{
printf("Name = %s",student[i].name);
printf("\n");
printf("Student_ID = %d",student[i].student_id);
printf("\n");
printf("Department = %s",student[i].department);
printf("\n");

}
}

You might also like