0% found this document useful (0 votes)
50 views

Lab Session 10 ROMAIL BUTT (19689) : Object Study of Structures Theory

The document discusses structures in C programming. It defines a structure called "personnel" with fields for name (char array) and agent number (int). It then declares a variable of this structure type, assigns values to the fields, and prints them out. It then asks the student to [1] define a structure called "student" with fields for name, ID, and marks and [2] declare an array of 5 students, assign specific values to the fields including student ID as their roll number.

Uploaded by

Umer Sheikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Lab Session 10 ROMAIL BUTT (19689) : Object Study of Structures Theory

The document discusses structures in C programming. It defines a structure called "personnel" with fields for name (char array) and agent number (int). It then declares a variable of this structure type, assigns values to the fields, and prints them out. It then asks the student to [1] define a structure called "student" with fields for name, ID, and marks and [2] declare an array of 5 students, assign specific values to the fields including student ID as their roll number.

Uploaded by

Umer Sheikh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy