BCP Unit-6 Wordpress PDF
BCP Unit-6 Wordpress PDF
UNIT: 6
Structure, Union and Files
Structure
Introduction to Structures and Declaration, Initialization and accessing of
Structures:
Structure is a collection of logically related data items with different data type.
Structure must be defined first, that may be used later to declare structure.
Syntax:
struct structure_name
{
datatype member1;
datatype member2;
---
datatype memberN;
};
struct is a keyword, it declares structure to hold data.
Structure_Name is the name of structure (structure tag).
member1, member2,… are members of structure.
Example:
struct student
{
char name[20];
int roll_no;
float per;
}s1;
Here s1 is a structure variable.
Structure Initialization:
Example:
struct student
{
char name[20];
int roll_no;
float per;
}s1={"Arav",20,85.5};
Example:
#include<stdio.h>
struct student
{
char name[20];
int roll_no;
float per;
}s1;
void main()
{
struct student s1={"Arav",20,85.5};
printf("\nName : %s",s1.name);
printf("\nRollNo : %d",s1.roll_no);
printf("\nPercent : %f",s1.per);
}
Example:
union student
{
char name[20];
int roll_no;
float per;
};
Example:
union student
{
char name[20];
int roll_no;
float per;
}u1;
void main()
{
u1.roll_no=65;
printf("\nRollNo : %d",u11.roll_no);
}
2. Any member can be accessed at any time. Only one member can be accessed at a time.
Array Structure
An array is a collection of variables of same A structure is a collection of variables of
1
data type known by same name. different data type known by same name.
For array we have to declare an array For structure we have to design and declare a
3. variable and use it. data structure before the variables of that type
are declared and used.
Example: Example:
int a[5]; struct data
{
4. int a;
float b;
char c;
};
For Example:
#include<stdio.h>
enum week {Mon=1, Tue, Wed, Thur, Fri, Sat, Sun};
void main()
{
enum week day;
day = Wed;
printf("%d",day);
getch();
}
Output:2
typedef :
The typedef is a keyword that is used in C programming to provide existing data types with a new name.
typedef keyword is used to redefine the name already the existing name.
When names of datatypes become difficult to use in programs, typedef is used with user-defined datatypes.
Syntax:
typedef <existing_name> <alias_name>
Example:
typedef long mylong
Prepared By: Department of Computer Engineering Page 4
Subject Name: Basic Computer Programming Unit No: 06 Subject Code: 4310702
Files
Introduction to text Files
File:
A file is a group of related data which is stored in a disk.
File operations:
(1) Naming a file
(2) Opening a file
(3) Reading data from a file
(4) Writing data to a file
(5) Closing a file
fp=fopen("E:\\abc.txt", "r");
---------------------
fclose();
char name[10]=”abc”;
int id=1;
FILE *fp;
Fp= fopen(“ E:\\ abc.txt”, “w”);
fprintf(fp,”%s %d”, name,id);
fclose(fp);
getch();
}
fscanf() : Read all data written in file and store in variables.
Syntax: fscanf(fp , “Control String “, List);
Where fp is a file pointer.
List may include variable, constants and string.
Example:
void main()
{
char name[10];
int id;
FILE *fp;
Fp= fopen(“ E:\\ abc.txt”, “r”);
fscanf(fp,”%s %d”, name, &id);
printf(“Name : %s and id : %d”, name, id);
fclose(fp);
getch();
}
Example of reading data from a file and writing to it(in text mode):
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
fp = fopen("hello.txt", "w");
printf("Enter data");
while( (ch = getchar()) != EOF) {
putc(ch,fp);
}
fclose(fp);
fp = fopen("hello.txt", "r");
fclose(fp);
}