Professional Documents
Culture Documents
Array Structure
Array is a collection of same datatype Structure is a collection of different datatype
elements. elements.
An array is a derived datatype. Structure is a programmer defined one.
Array elements are referred by subscript. Structure elements are referred by their unique
name.
Array elements are accessed by their position Structure elements are accessed by their object
or subscript. as ‘.’ operator.
Does not have bit fields. May contain bit fields.
It is not a keyword. It is a keyword.
Defining a Structure:
● The general form of structure definition is
Syntax:
Struct Structure_name
{
datatype element1;
datatype element2;
datatype element n;
}; StructStructure_name var1, var2;
Struct Structure_name
{
datatype element1;
datatype element2;
datatype element n;
} var1, var2;
Example:
Struct Student{
char grade; floatavg; intrno;
};
intrno;
Struct Student S1; floatavg;
} S1;
Struct Student
{
char grade;
Initialization of a Structure:
● Structure elements can be initialized as follows,
Example:
Struct Student Struct Student
{ {
intrno; intrno;
int tot; int tot;
floatavg; floatavg;
} } S1={101, 486, 97.26};
Struct Student S1={101, 486,97.26};
● The amount of the memory space allocated to it is equal to the sum of the memory
space required by all of its members.
Example:
Struct Student Struct Student
{ {
char grade; 1 char name[20]; 20
int tot; 2 int tot; 2
floatavg; 4 floatavg; 4
} S1; } S1;
Size of the Structure = 7 bytes Size of the Structure = 26 bytes
(1+2+4 bytes) (20+ 2+4 bytes)
Example:
struct stud
{
…………
…………
}s1,* s2;
s2 = &s1;
s1 Structure Variable
s2 Structure Pointer
Advantages of pointer to structure:
1. It is easier to manipulate the pointers to structure than manipulating structures
themselves.
2. Some data structures (linked list, trees…) use the structures containing pointers to
structures.
3. Passing a pointer to a structure as any argument to a function is afficient as compared
to passing a structure to a function.
Example Program: Student details using pointers to structures:
#include<stdio.h>
struct student
{
intrno;
int m1, m2, m3;
floatavg;
};
void main()
{
struct student s1={ 101,80,90,90,86.66}
struct student *s2 = &s1;
clrscr();
printf(“details are”);
printf(“%d %d %d %d %f”, s2rno, s2m1, s2m2, s2m3,s2avg);
getch();
}
Output:
details are
101 80 90 90 86.66
Array of structures:
● It is possible to create an array whose elements are of structure type, such an array is
known as an array of structure. (i.e) If there is a need to access more no. of structure
variables, then they can use structure variable as arrays.
Example:
struct student
{
intrno;
floatavg;
}s[50];
● Here the structure variable ‘s’ can store 50 student details.
● In such cases the structure elements are accessed with for loop as s[i].rno, s[i].avg.
(i.e) for (i=0;i<50;i++)
{
scanf(“%d %f”, &s[i].rno, &s[j].avg);
}
Example Program: Student details using structures
#include<stdio.h>
#include<conio.h>
struct student
{
char name[10];
intrno, m1, m2, m3, tot;
floatavg;
}s[50];
void main()
{
int n, i;
clrscr();
printf(“Enter the number of students \n”);
scanf(“%d”, &n);
printf(“Enter Name, Regno., 3 Marks \n”);
for(i=0;i<n;i++)
scanf(“%s %d %d %d %d”, &s[i].rno, &s[i].m1, &s[i].m2, &s[i].m3);
s[i].tot= s[i].m1+s[i].m2+s[i].m3;
s[i].avg= s[i].tot/3;
}
printf(“\n \n Student Mark List \n”);
printf(“********************************\n ”);
printf(“\n Name \t Regno \t Total \t Average \n”);
for(i=0;i<n;i++)
{
printf(“\n %s \t %d \t %d\t %0.2f”, s[i].name, s[i].rno, s[i].tot, s[i].avg);
}
getch();
}
Output:
Enter the number of students 2
Enter Name, Regno., 3 Marks
abc 101 90 90 90
xyz 102 80 80 80
Student Mark List
******************
Name Regno Total Average
abc 101 270 90.00
xyz 102 260 80.00
OUTPUT:
Name is:
Percentage is: 86.500000
static Static RAM Local Zero Till the main program ends.
It retains the available value
between various function
calls.
Syntax:
storage class specifier data type variables;
Register:
● Are special storage classes within a computer’s CPU instead of storing in RAM or
main memory.
● Register specifies local scope.
Syntax: register datatype var1, var2,….varn;
Example: register int a=10;
Example Program:
#include<stdio.h>
#include<conio.h>
void main()
{
registerint a=10;
printf(“a is %d”, a);
}
Output:
a is 10
File is a structure defined in ‘stdio.h’ to handle file operations. Various operations like
opening the file reading/writing in a file and closing the file can be done.
The operations that can be carried out on files in C language are as follows −
● Naming the file.
● Opening the file.
● Reading from the file.
● Writing into the file.
● Closing the file.
There are several naming conventions that are more or less in widespread use. This
includes prefixing or suffixing ptr or p to the name of the variable identifier.
Syntax
The syntax for opening and naming file is as follows −
FILE *File pointer;
For example, FILE * fptr;
File pointer = fopen ("File name”, "mode”);
To Open a File:
● The syntax for opening the file is: File* fopen(char* name, char* mode);
● Opens the file ‘name’ and returns the FILE pointer.
● Values for ‘mode’ are ‘r’,’w’and ‘a’.
● fopen returns NULL if its unable to open file.
● If file does not exist, fopen will create file, if mode is ‘w’ or ‘a’. We can use one of
the following modes in the fopen() function.
Mode Description
r opens a text file in read mode
w opens a text file in write mode
a opens a text file in append mode
r+ opens a text file in read and write mode
w+ opens a text file in read and write mode
a+ opens a text file in read and write mode
rb opens a binary file in read mode
wb opens a binary file in write mode
ab opens a binary file in append mode
rb+ opens a binary file in read and write mode
wb+ opens a binary file in read and write mode
ab+ opens a binary file in read and write mode
Eg :
#include<stdio.h>
void main( )
{
FILE *fp ;
char ch ;
fp = fopen("file_handle.c","r") ;
while ( 1 )
{
ch = fgetc ( fp ) ;
if ( ch == EOF )
break ;
printf("%c",ch) ;
}
fclose (fp ) ;
}
Eg:
#include <stdio.h>
main()
{
FILE *fp;
fp = fopen("file1.txt", "w");//opening file
fputc('a',fp);//writing single character into file
fclose(fp);//closing file
}
Eg:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char c;
clrscr();
fp=fopen("myfile.txt","r");
while((c=fgetc(fp))!=EOF){
printf("%c",c);
}
fclose(fp);
getch();
}
Eg:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char text[300];
clrscr();
fp=fopen("myfile2.txt","r");
printf("%s",fgets(text,200,fp));
fclose(fp);
getch();
}
Close a File – int fclose(FILE* fp);
The fclose() function is used to close a file. The file must be closed after performing all the
operations on it.
Eg:
#include <stdio.h>
main(){
FILE *fp;
fp = fopen("file.txt", "w");//opening file
fprintf(fp, "Hello file by fprintf...\n");//writing data into file
fclose(fp);//closing file
}
Sequential File
A sequential file is a type of file structure used in computer systems to organize and store
data in a sequential order. In a sequential file, records are stored one after another, with each
new record being appended to the end of the file. This means that the records are stored in the
order they are added, and accessing the data follows a sequential pattern from the beginning
to the end of the file. In a sequential file, the records have a fixed length or are
variable-length with a marker indicating the end of each record. When reading or writing data
in a sequential file, the file pointer moves sequentially through the file, starting from the first
record and progressing through each subsequent record.
Access Speed Sequential access files are Random access files, on the
slower compared to random other hand, allow direct
access files since accessing a access to specific records,
specific record requires resulting in faster access
reading through all the times.
previous records in the file
Access Method Sequential access files allow Random access files allow
access to records in a direct access to specific
sequential manner records using an index,
record number, or key.
Record Ordering Sequential access files store Random access files do not
records in a specific order, have any specific order of
usually the order in which storing records.
they were added to the file.
Insertion of new records Inserting a new record in a Random access files may
sequential access file is require relocating other
relatively easy since new records to maintain the order
records are added to the end so insertion becomes hard as
of the file. compared to sequential
access.