0% found this document useful (0 votes)
2 views12 pages

Structure

Module 5 of the Principles of Programming Using C covers the concepts of structures and file management. It explains how to define and manipulate structures, access their members, and initialize structure variables, along with examples of nested structures and arrays of structures. Additionally, it discusses file management concepts including file I/O functions and the process of opening and closing files.

Uploaded by

khushbu
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)
2 views12 pages

Structure

Module 5 of the Principles of Programming Using C covers the concepts of structures and file management. It explains how to define and manipulate structures, access their members, and initialize structure variables, along with examples of nested structures and arrays of structures. Additionally, it discusses file management concepts including file I/O functions and the process of opening and closing files.

Uploaded by

khushbu
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

PRINCIPLES OF PROGRAMMING Using C Module-5 BPOPS103

P r inciples of
P rog r am m i ng
Using C

SUB CODE: BPOPS103

MODULE- 5

 STRUCTURES
 FILE MANAGEMENT

Dr. Santosh K C, CSE dept., BIET, DVG 1


PRINCIPLES OF PROGRAMMING Using C Module-5 BPOPS103

MODULE-5
STRUCTURES AND FILES
LEARNING OBJECTIVES

o Explain how structures are used in C.


o Describe how structure variable and members are manipulated.
o Know the concept of File
o Discuss Files I/O functions
o Opening and closing of Files

Basics of Structure:
 “A Structure is a user defined data type, which is used to store the values of different
data types together under the same name”.
or
 “A structure is a collection of one or more variables of same data type or dissimilar
data types grouped under a single name for convenient handling”.
 Each member of a structure is assigned a separate memory location.
 A structure helps us to organize complicated data, particularly in large programs, because
they permit a group of related variables to be treated as a unit instead of as separate entities.

Defining a Structure:
 Definition of structures starts with the ‘struct’ keyword, followed by the name of the
structure, a pair of curly braces containing declaration of a set of variables called structure
members and then semicolon at the end.

Syntax:
struct structure_name
{
data_type1 variable1;
data_type2 variable2;
………………………… structure members
…………………………
data_typen variablen;
};

 Struct: It is a keyword, and it indicates the beginning of a structure definition.


 Structure: It represents the name of the structure being defined.
 Data_type1 to n: Represent the data types the structure members.
 Variables1 to n: Represent the variables in the Structure members.

Dr. Santosh K C, CSE dept., BIET, DVG 2


PRINCIPLES OF PROGRAMMING Using C Module-5 BPOPS103
Examples:
1. struct employee
{
int emp_id, emp_age;
char name[20], grade;
float emp_salary;
};

2. struct book
{
char book_title[20];
char author[20];
float cost_of_book;
int pages;
};

Declaring Structure Variables:


 We can declare variables of the structure name in the same way as we declare variables of
predefined data type.

Syntax:
struct structure_name variable_name;
Example:
struct student
{
int roll_no, age; Structure template with tag
char name[20], grade; Here student is the
tag
float collg_fee;
};
struct student student1; /*student1 is structure variable

 We can also declare multiple structure variables using a single line of code by using
following syntax:

Syntax:
struct structure_type variable1,variable2,………….variablen;

Example: struct student


{
int roll_no, age;
char name[20], grade;
float collg_fee;
};
struct student student1,student2;

 The definition of a structure and declaration of structure variables can be done


together.

Dr. Santosh K C, CSE dept., BIET, DVG 3


PRINCIPLES OF PROGRAMMING Using C Module-5 BPOPS103
Syntax:
struct structure_name
{
data_type1 variable1;
data_type2 variable2;
…………………………
…………………………
data_type n variablen;
} structure_variable1, structure_variable2,………, structure_variablen;

Example: struct student


{
int roll_no, age;
char name[20], grade;
float collg_fee;
}student1,student2;

 Structure name is student and we have declared two variables of type student: student1 and
student2.

Accessing members of a Structure:


 A member of a structure is identified and accessed using the (.) Dot Operator. The Dot
Operator connects the member name to the name of its containing Structure.

Syntax:
structure_variable_name.member;

Example: student1.roll_no;

[Link];

Initializing Structure variable:


 Each structure variable contains a copy of all the members of the structure.
 Initializing a structure variable involves assigning values to its structure members.
 We can assign values to the members of the structure variables either all at once or one at a
time.
i. In first approach, the declaration of the structure variable and the initialization of
the structure members are done using a single line of code.
Example: struct student
{
int roll_no, age;
char name[20], grade;
float collg_fee;
};
struct student student1={50,18, “Rama”, ‘A’,35500.00};

Dr. Santosh K C, CSE dept., BIET, DVG 4


PRINCIPLES OF PROGRAMMING Using C Module-5 BPOPS103
ii. In the second approach, we first need to declare the structure variables and then
initialize each member of the structure variable one by one.
 Initializing each structure members separately requires us to access individually structure
members. We can access a structure member by dot (.) operator.

Syntax : structure_variable_name.member_name=value;

structure_name: It represents the name of the Structure variable.


member_name: It represents a member of the Structure variable.
value: It represents the value being assigned to the Structure member.
Example: struct student
{
int roll_no, age;
char name[20],grade;
float collg_fee;
} student1;
student1.roll_no=50;
[Link]=18;
[Link]= “Rama”;
[Link]= ‘A’;
student1.collg_fee=35500.00;

Example: Write a C program to print the details of student using structure.


#include <stdio.h>
#include<conio.h>
struct student
{
int roll_no, age;
char name[20], grade;
float collg_fee;
};
void main( )
{

struct student student1={50,18, “Rama”, ‘A’,35500.00};


clrscr();
printf(“Student Details:\n”);
printf(“Roll Number:%d\n”,student1.roll_no);
printf(“Age:%d\n”,[Link]);
printf(“Name:%s\n”,[Link]);
printf(“Grade:%c\n”,[Link]);
printf(“College Fee:%f\n”,student1.collg_fee);
getch();
}

Structures and Functions:


Dr. Santosh K C, CSE dept., BIET, DVG 5
PRINCIPLES OF PROGRAMMING Using C Module-5 BPOPS103
 We can pass an entire structure as a parameter to a function instead of passing individual
structure members.
 Structures are passed by value and enable a function to alter a parameter and return a value
as structure.
 Function definition must consist of declaration of a Structure and Structure variable as its
formal parameter.
 The Structure variables used in both calling function (actual parameter) and in called
function (formal parameter) belong to same Structure name or Structure template.
Example: Write a C program to pass a structure as a parameter to a function.
#include <stdio.h>
#include<conio.h>
struct student
{
int roll_no, age;
char name[20], grade;
float collg_fee;
};
void main ( )
{
struct student st;
printf(“Enter the student Details:\n”);
printf(“Enter the student Roll_no:”);
scanf(“%d”,&st.roll_no); OUTPUT:
printf(“Enter the student Age:”); Enter the student Details:
scanf(“%d”,&[Link]); Enter the student Roll_no: 10
printf(“Enter the student Name:”); Enter the student Age: 18
scanf(“%s”,[Link]); Enter the student Name: Rama
printf(“Enter the student Grade:”); Enter the student Grade: A
scanf(“%c”,[Link]); Enter the College fee: 35500
printf(“Enter the College fee:”); Student details:
scanf(“%f”,&st.collg_fee); Roll_no: 10
print_details(st); Age: 18
getch(); Name: Rama
} Grade: A
void print_details(st) College fee: 35500.00
{
printf(“Student details:\n”);
printf(“Roll_no:%d\n Age:%d\n Name:%s \n Marks:%d\n College
fee:%f”,st.roll_no,[Link],[Link]);
}
 Here structure named ‘student’ is defined and then the variable of that structure st has been
declared. This structure variable st is then passed as a parameter to the print_details ()
function.

Nested Structures:
 We can nest a structure inside another Structure.
 The structures can be nested in two ways.
1. In first approach, the complete definition of a structure is placed inside the definition of
another structure.
Example: struct student

Dr. Santosh K C, CSE dept., BIET, DVG 6


PRINCIPLES OF PROGRAMMING Using C Module-5 BPOPS103
{
int roll_no,age;
char name[20],grade;
float collg_fee;
struct date
{
int day, month, year;
} dob;
}student1;
2. In the second approach, the structures are defined separately and a variable of a Structure
type is declared inside the definition of another Structure.
Example: struct date
{
int day, month, year;
} dob;
struct student
{
int roll_no,age;
char name[20],grade;
float collg_fee;
struct date dob;
} student1;
 We can access the variables of a structure type that are nested inside another structure in the
same way as we access other members of that structure.

Example: Write a C program to print the student details using nested structures.
#include <stdio.h>
#include<conio.h>

struct student
{ OUTPUT:
int roll_no; Enter the student Details:
char name[20], grade; Enter the student Roll_no: 10
float collg_fee; Enter the student Name: Rama
struct date Enter the student Grade: A
{ Enter the College fee: 35500
int day,month,year; Enter the Date of Birth in dd/mm/yyyy
}dob; format: 13 05 1989
}; Student details:
Roll_no: 10
void main ( ) Name: Rama
{ Grade: A
College fee: 35500.00
struct student student1; Date of Birth: 13/05/1989
printf(“Enter the student Details:\n”);
printf(“Enter the student Roll_no:”);
scanf(“%d”,&student1.roll_no);
printf(“Enter the student Name:”);

Dr. Santosh K C, CSE dept., BIET, DVG 7


PRINCIPLES OF PROGRAMMING Using C Module-5 BPOPS103
scanf(“%s”,[Link]);
printf(“Enter the student Grade:”);
scanf(“%c”,[Link]);
printf(“Enter the College fee:”);
scanf(“%f”,&student1.collg_fee);
printf(“Enter the Date of Birth in dd/mm/yyyy format:”);
scanf(“%d%d%d”,&[Link], &[Link], &[Link]);
printf(“Student Details:\n”);
printf(“Roll_no:%d\n”,student1.roll_no);
printf(“Name:%s\n”,[Link]);
printf(“Grade:%c\n”,[Link]);
printf(“College fee:%f\n”,student1.collg_fee);
printf(“Date_of_Birth:%d/%d/%d\n”,[Link],[Link],[Link]
[Link]);
getch();
}

Array of Structures:

 Similar to creating arrays of a predefined data type such as int, float and char, we can also
create an array of structure type.
 Arrays of Structure type are required in situations when we need to apply the same
Structure to a set of Objects.
Example: Defining a Structure and then creating an array of Structure type:
struct student
{
int roll_no,age;
char name[10],grade;
float collg_fee;
}students[20];

 Here we have created a structure called ‘student’ with 5 fields: roll_no, age, name, grade
and collg_fee and array of structure type of size 20(We can store the details of 20 students).

Example:

1. Write a C program to print the details of students using array of structures.


#include<stdio.h>
#include<conio.h>
struct student
{
int roll_no,age;
char name[10],grade;
float collg_fee;
}students[20];

void main()
{
int i;
OUTPUT:
Dr. Santosh K C, CSE dept., BIET, DVG Enter the details of 3 Students: 8
Enter the Roll_no:101
Enter the age: 18
PRINCIPLES OF PROGRAMMING Using C Module-5 BPOPS103
printf(“Enter the details of 3 Students:\n”);
for(i=0;i<3;i++)
{
printf(“Enter the Roll_no:”);
scanf(“%d”,&students[i].roll_no);
printf(“Enter the age:”);
scanf(“%d”,&students[i].age);
printf(“Enter the Name:”);
scanf(“%s”,students[i].name);
printf(“Enter the Grade:”);
scanf(“%c”,students[i].grade);
printf(“Enter the College fee:”);
scanf(“%f”,&students[i].collg_fee);
}
printf(“Student details are:\n”);

for (i=0;i<3;i++)
{
printf(“Roll Number:%d\n”,students[i].roll_no);
printf(“Age:%d\n”,students[i].age);
printf(“Name:%s\n”,students[i].name);
printf(“Grade:%c\n”,students[i].grade);
printf(“College fee:%f\n”,students[i].collg_fee);
}
getch();
}

2. Write a C program to maintain a record of n student details using an array of


structures with four fields (Roll number, Name, Marks, and Grade). Assume appropriate
data type for each field. Print the marks of the student, given the student name as input.
#include<stdio.h>
#include<conio.h>
struct student
{
int rollno,marks;
char name[20],grade;
};
void main()
{
int i,n,found=0;
struct student s[10];
char sname[20];

Dr. Santosh K C, CSE dept., BIET, DVG 9


PRINCIPLES OF PROGRAMMING Using C Module-5 BPOPS103
printf("Enter the number of student details n=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the %d student details:\n",i+1);
printf("Enter the Roll number:\n");
scanf("%d",&s[i].rollno);
printf("Enter the student name:\n");
scanf("%s",s[i].name);
printf("Enter the marks:\n");
scanf("%d",&s[i].marks);
printf("Enter the grade:\n");
scanf("%c",&s[i].grade);
}
printf("\n Student details are:\n");
printf("\nRollno\t\tName\t\tMarks\t\tGrade\n");
for(i=0;i<n;i++)
{
printf("%d\t\t%s\t\t%d\t\t%c\n",s[i].rollno,s[i].name,s[i].marks,s[i].grade);
}
printf("Enter the student name to print the marks:");
scanf("%s",sname);
for(i=0;i<n;i++)
{
if(strcmp(s[i].name,sname)==0)
{
printf("Marks of the student is:%d",s[i].marks);//s[1].marks
found=1;
}
}
if(found==0)
printf("Given student name not found\n");
getch();
}

OUTPUT:
Enter the number of student details n=3
Enter the 1 student details:
Enter the Roll number: 123
Enter the student name: Abhi
Enter the marks: 90
Enter the grade: A

Enter the 2 student details:


Enter the Roll number: 124
Enter the student name: Bharath
Enter the marks: 65
Enter the grade: B

Enter the 3 student details:

Dr. Santosh K C, CSE dept., BIET, DVG 10


PRINCIPLES OF PROGRAMMING Using C Module-5 BPOPS103
Enter the Roll number: 125
Enter the student name: Chethan
Enter the marks: 50
Enter the grade: C

Student details are:


Rollno Name Marks Grade
S[0] 123 Abhi 90 A
S[1] 124 Bharath 65 B
S[2] 125 Chethan 50 C

Enter the student name to print the marks: Bharath


Marks of the student is: 65

The typedef Statement: (keyword)


 The ‘typedef’ statement allows us to create a new data type from an existing data type.
 The new data type has a different name but same characteristics as that of the existing data
type.
 We create a new data type from both a predefined data types (int, float, char) and a user
defined data type, such as structure.
 We can use the new data type to declare variables and arrays of the original data type.
 The Scope of the new data type is limited to the function in which it is to be created.
Syntax:
typedef old_data_type new_data_type;
‘typedef’ is a keyword.
old_data_type: is the name of the original data type.
new_data_type: is the name of the new data type.
Example: typedef int biet;
biet a, b, c;
biet a[10];
 Here first it creates a new data type called ‘integer’ from predefined data type int and
declares 3 variables (a, b, c) and an array of type integer.

Example: Write a C program to print the Employee Details using type def statement.
#include<stdio.h>
#include<conio.h>
struct employee
{
int emp_id;
char name[20];
float salary;
};

void main()
{

typedef struct employee employee1;


employee1 emp;
clrscr();
Dr. Santosh K C, CSE dept., BIET, DVG 11
PRINCIPLES OF PROGRAMMING Using C Module-5 BPOPS103
printf(“Enter the Employee id:\n”);
scanf(“%d”,&emp.emp_id);
printf(“Enter name of the Employee:\n”);
scanf(“%s”,[Link]);
printf(“Enter salary of the Employee:\n”);
scanf(“%s”,[Link]);
printf(“Employee Details:\n”);
printf(“Employee ID:%d\n”,emp.emp_id);
printf(“Name:%s\n”,[Link]);
printf(“Salary:%f\n”,[Link]);
getch();
}

OUTPUT:
Enter the Employee id: 100
Enter name of the Employee: manu
Enter salary of the Employee: 10000
Employee Details:
Employee ID: 100
Name: manu
Salary: 10000.00

Dr. Santosh K C, CSE dept., BIET, DVG 12

You might also like