unit - 5 programming in c
unit - 5 programming in c
1st – Semester - I
Define structure:
Structure is a derived data type to organize a group of related data items of different data
types referring to a single entity.
Single variable capable of holding data items of different data types.
Structure variable is an object consisting of a sequence of named elements of various data
types.
Structure have declaration and definitions.
The declaration of a structure does not reserve any storage space but the definition of a
structure creates structures variables.
Syntax:
Struct tag
{
declaration of member1;
declaration of member2;
…..
….
declaration of membern;
}
The declaration begins with the keyword struct.
The list of declarations of its member must be enclosed in braces.
The tag is an identifier and it is optional.
Example:
Struct passenger
{
char name[30];
int age;
int train_no;
char destn[20];
char board_place[20];
}
There are 5 members in this structure declaration.
It describes the blueprint or template or shape of a structure.
Tag is passenger.
The tag name may be used as an ordinary variable name or a member variable name
without any conflict.
Example:
Struct student
{
char studname[25];
int rollno;
int mark;
} stud;
char student;
int mark;
the tag and character variable share the same name student.
Definition of structure variable:
Struct tag var1,var2,…..,varn;
For example:
Struct passenger pas1,pas2;
Pas1 and pas2 are structure data type the structure definition reserve spaces for the
structure variables.
Different ways to define structure variables:
1. Struct tag 2.struct tag
{ {
member declarations; member declarations;
}; };
struct tag var1,var2,….,varn; typedef struct tag newname;
newname var1,var2,…..,varn;
3./* Structure declaration */ 4. struct tag
/* with renaming */ {
typedef struct member declarations;
{ } var1,var2,var3,…,varn;
member declarations;
} newname;
newname var1,var2,….,varn;
5. struct
{
member declarations;
} var1,var2,…,varn;
The blank space is optional after the closing braces in first, second, fourth and fifth
method.
But there should be atleast one blank spaces after closing braces in third method.
Declaration part defines the template of the structure and it does not reserve the memory
spaces.
The structure variables are created and storage spaces are reserved.
INTIALISATION OF STRUCTURE VARIABLES:
A structure variable can be initialized in its definition itself with a list of initializers
enclosed within the braces.
Each initialiser must be a constant expression and the order and type of each member
must match the order and type of its declaration.
Example:
Struct passenger pas1={“Priya”,24,6500,”attur”,”salem”};
Struct passenger pas2={“Uma”,25,5200,”salem”,”Chennai”};
Example3:
Structure passenger pas3=pas1;
Assigns the data of pas1 to pas3.
But the structure variable at the right side of the assignment statement has to be defined
and initialized or read before the assignment.
Example:
Struct employee
{
int empno;
char e_name[20];
}emp1={2,”guna”},emp2={32=”kalki”};
Accessing the members of a structure:
Each member of a structure variable can be accessed using the structure member operator dot(.).
Syntax:
Structure_variable.member
Example:
Members name and age of the structure passenger can be accessed as
Pas1.name /* refers to name of structure variable pas1 */
Pas2.age
It is possible to assign values to all the individual members using the assignment
statement.
Pas1.age=18;
Pas1.train_no=6020;
The following assignment is incorrect since the member name is an array name which
cannot be initialized.
Pas1.name=”MALINI.R”; /* Invalid Assignment */
The possible way assigning a string to a character array is
Form1:
Pas1.name[0]=’M’;
Pas1.name[1]=’A’;
Pas1.name[2]=’L’;
Pas1.name[3]=’I’;
Pas1.name[4]=’N’;
Pas1.name[5]=’I’;
Pas1.name[6]=’\0’;
Form 2:
Function scanf() may be used
Scanf(“%s”,pas1.name);
Form 3:
Function strcpy() may be used to copy a string
Strcpy(pas1.name,”MALINI”);
Example program:
Main()
{
typedef struct student
{
char name[20];
int rollno;
int mark;
}STUDENT;
STUDENT s1,s2; /* Structure definition */
Printf(“Enter the values of each member in structure s1 \n”);
Printf(“Enter the name :\n”);
Scanf(“%s”,s1.name);
Printf(“Enter the roll number:\n”);
Scanf(“%d”,&s1.rollno);
Printf(“Enter the marks :\n”);
Scanf(“%d”,&s1.mark);
Printf(“The members of structure S1:\n”);
Printf(“Name :%s\n”,s1.name);
Printf(“Roll number :%d\n”,s1.rollno);
Printf(Mark : %d”,s1.mark);
/* Assignment of one structure to another structure */
s2=s1;
printf(“%s,%d,%d\n”,s2.name,s2.rollno,s2.mark);
}
Nested structure:
If a structure contains one or more structure as its members, it is known as a nested
structure.
Example:
Structure date
{
int day;
char month[10];
int year;
};
The structure date may be used as a member in another structure as given below.
Example:
Man.dob.day /* refer to the member day of the inner structure variable dob */
#include<stdio.h>
Main()
{
typedef struct
{
Int date;
Char month[10];
Int year;
}DATE;
Typedef struct
{
Char name[25];
Int age;
Char sex;
Date dob;
}person;
Printf(“Enter the name, age and sex \n”);
Scanf(“%s%d%c”,man.name,&man.age,&man.sex);
Printf(“Enter the date of birth : date, month in words and year\n”);
Scanf(“%d%s%d”,&man.dob.date,man.dob.month,&man.dob.year);
Printf(“Enter the name, age and sex \n”);
Printf(“%s%d%c”,man.name,man.age,man.sex);
Printf(“Enter the date of birth : date, month in words and year\n”);
printf(“%d%s%d”,man.dob.date,man.dob.month,man.dob.year);
}
Arrays of Structure:
A group of structures may be organized in an array resulting in an array of structures.
Each element in the array is a structure.
Example:
Struct person
{
Char name[30];
int age;
char sex;
struct date dob;
};
Struct person emp[10];
defines an array of 10 structures.
Each structure variable emp[0],emp[1],…,emp[9] contains structure as its value.
The members are accessed as
emp[0].age emp[0].sex emp[0].dob.month
emp[1].age emp[1].sex emp[1].dob.month
. . .
. . .
. . .
emp[9].age emp[9].sex emp[9].dob.month
The character array can be accesses as
emp[0].name[0]=’M’;
emp[0].name[1]=’A’;
An array of structures can also be initialized similar to arrays.
Struct person emp[10]={
{“Priya”,8,’F’},
{“Uma”,10,’F’}
};
Initializes two structure variables emp[0] and emp[1] partially.
/* Program using an array of structures in functions */
#include<stdio.h>
typedef struct
{
char name[10];
int regno;
char major[10];
char result[10];
} STUDENT;
main()
{
int n;
STUDENT stud[10];
Printf(“Enter the number of student \n”);
Scanf(“%d”,&n);
Readrecord(stud,n);
Writerecord(stud,n);
}
Readrecord(STUDENT stud[],int n)
{
int i;
for(i=0;i<n;i++)
{
Printf(“Enter the name and register number of stud[%d]\n”,i);
Scanf(“%s%d”,stud[i].name,&stud[i].regno);
Printf(“Enter the major and result of stud[%d]\n”);
Scanf(“%s%s”,stud[i].major,stud[i].result);
}
}
Writerecord(STUDENT stud[],int n)
{
int i;
printf(“the details of student records:\n”);
for(i=0;i<n;i++)
{
printf(“%s%d”,stud[i].name,stud[i].regno);
printf(“%s%s\n”,stud[i].major,stud[i].result);
}
}
FILE MANAGEMENT IN C
Introduction:
R Functions such as scanf and printf to read and write data.
R These are console oriented I/O Functions, which always use the
terminal as the target place.
R This works fine as long as the data is small.
Disadvantages:
R It take time consuming to handle large volumes of data through terminals.
R The entire data is lost when either the program is terminated or the
computer is turned off.
R More flexible approach where data can be stored on the disks and read
whenever necessary, without destroying the data.
R A file is a place on the disk where group of related data is stored.
Basic file
Operations:
1) Naming a file
2) Opening a file.
3) Reading data from the file.
4) Writing data to a file and
5) Closing a file.
Fopen() Creates a new file for use opens an existing file for use.
Fclose() Closes a file which has been opened for use.
Getc() Reads a character from a file.
Putc() Writes a character to a file.
Fprintf() Writes a set of data values to a files
Fscanf() Reads a set of data values from a files
Getw() Reads an integer to a file
Putw() Writes an integer to a file
Fseek() Sets the position to the desired point in the file.
Ftell() Gives the current position in the file
Rewind() Set the position to the beginning of the file
If we want to store data in a file in the secondary memory, we must specify the
R Filename
R Data structure
R Purpose to the operating system
1) Primary name
2) Extension
Example:
Student.c Text.out
R When we open a file, we must specify what we want to do with the file.