Вы находитесь на странице: 1из 7

Practical no.

7
To study and perform program based on the use of Structures.

Theory:
C Structure is a collection of different data types which are grouped together and each element in
a C structure is called member.

BELOW TABLE EXPLAINS FOLLOWING CONCEPTS IN C STRUCTURE.


1. How to declare a C structure?
2. How to initialize a C structure?
3. How to access the members of a C structure?
Using normal variable

Syntax:
struct tag_name
{
data type var_name1;
data type var_name2;
data type var_name3;
};

Example:
struct student
{
int mark;
char name[10];
float average;
};

Declaring structure using normal


variable:
struct student report;

Initializing structure using normal


variable:
struct student report = {100, “Mani”,
99.5};

Accessing structure members using


normal variable:
report.mark;
report.name;
report.average;
EXAMPLE PROGRAM FOR C STRUCTURE:
This program is used to store and access “id, name and percentage” for one student. We can also
store and access these data for many students using array of structures

#include <stdio.h>
1
#include <string.h>
2
3
struct student
4
{
5
int id;
6
char name[20];
7
float percentage;
8
};
9
10
int main()
11
{
12
struct student record = {0}; //Initializing to null
13
14
record.id=1;
15
strcpy(record.name, "Raju");
16
record.percentage = 86.5;
17
18
printf(" Id is: %d \n", record.id);
19
printf(" Name is: %s \n", record.name);
20
printf(" Percentage is: %f \n", record.percentage);
21
return 0;
22
}
23
OUTPUT:
Id is: 1
Name is: Raju
Percentage is: 86.500000

Array of Structures:
C Structure is collection of different datatypes ( variables ) which are grouped together. Whereas,
array of structures is nothing but collection of structures. This is also called as structure array in
C.

EXAMPLE PROGRAM FOR ARRAY OF STRUCTURES IN C:


This program is used to store and access “id, name and percentage” for 3 students.
Structure array is used in this program to store and display records for many students. You can
store “n” number of students record by declaring structure variable as ‘struct student record[n]“,
where n can be 1000 or 5000 etc.
1 #include <stdio.h>

2 #include <string.h>

4 struct student

5 {

6 int id;

7 char name[30];

8 float percentage;

9 };

10

11 int main()

12 {

13 int i;

14 struct student record[2];

15

16 // 1st student's record

17 record[0].id=1;

18 strcpy(record[0].name, "Raju");

19 record[0].percentage = 86.5;

20

21 // 2nd student's record

22 record[1].id=2;

23 strcpy(record[1].name, "Surendren");
24 record[1].percentage = 90.5;

25

26 // 3rd student's record

27 record[2].id=3;

28 strcpy(record[2].name, "Thiyagu");

29 record[2].percentage = 81.5;

30

31 for(i=0; i<3; i++)

32 {

33 printf(" Records of STUDENT : %d \n", i+1);

34 printf(" Id is: %d \n", record[i].id);

35 printf(" Name is: %s \n", record[i].name);

36 printf(" Percentage is: %f\n\n",record[i].percentage);

37 }

38 return 0;

39 }

OUTPUT:
Records of STUDENT : 1
Id is: 1
Name is: Raju
Percentage is: 86.500000

Records of STUDENT : 2
Id is: 2
Name is: Surendren
Percentage is: 90.500000
Records of STUDENT : 3
Id is: 3
Name is: Thiyagu
Percentage is: 81.500000

Program to add two distances in inch-feet system

#include <stdio.h>

struct Distance

int feet;

float inch;

} d1, d2, sumOfDistances;

int main()

printf("Enter information for 1st distance\n");

printf("Enter feet: ");

scanf("%d", &d1.feet);

printf("Enter inch: ");

scanf("%f", &d1.inch);

printf("\nEnter information for 2nd distance\n");

printf("Enter feet: ");

scanf("%d", &d2.feet);

printf("Enter inch: ");


scanf("%f", &d2.inch);

sumOfDistances.feet = d1.feet+d2.feet;

sumOfDistances.inch = d1.inch+d2.inch;

// If inch is greater than 12, changing it to feet.

if (sumOfDistances.inch>12.0)

sumOfDistances.inch = sumOfDistances.inch-12.0;

++sumOfDistances.feet;

printf("\nSum of distances = %d\'-%.1f\"",sumOfDistances.feet, sumOfDistances.inch);

return 0;

Output

Enter information for 1st distance

Enter feet: 23

Enter inch: 8.6

Enter information for 2nd distance

Enter feet: 34

Enter inch: 2.4

Sum of distances = 57'-11.0"


In this program, a structure Distance is defined. The structure has two members inch (a float)
and feet (an integer).

Two variables (d1 and d2) are created which stores two distances (in inch and feet). Then, the
sum of two distances is stored in sumOfDistances structure and displayed on the screen.

Conclusion:
In this way we had studied and perform program based on structure.

Вам также может понравиться