0% found this document useful (0 votes)
21 views

Assignment 6

Her name is amtul

Uploaded by

zdo83247
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Assignment 6

Her name is amtul

Uploaded by

zdo83247
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Part-A

1.What is a structure?
Ans: A structure is a collection of logically related variables of different data types
under a single name.
2.What is a file?
Ans. A file is a complex data type that is stored externally to the main memory,
usually on disk. In other words we can say, it is a storage area of memory, where
the data are stored in an organized form.
3.Define nested structure
Ans. when a structure is placed within another structure, as its member, then the
resultant structure is called nested structure.
4.write the syntax to open a file
Ans. Syntax: FILE *fp;
Fp=fopen(“file_name” mode);

5. write the syntax to close a file


Ans. Syntax: Fclose (FILE *fp);
6.write a c code to open a text file in write mode
Ans. #include<stdio.h>
void main()
{
FILE*p;
p =fopen(“file 01.txt”,”w”);
}
7.define array of structure
Ans.An array whose elements are of type structure is called array of structure. It is
generally useful when we need multiple structure variables in our program.
8.list functions that are used to read and write on binary file
Ans. Wb(write)
Rb(read)
Ab(write and append)
W+b(read+write)
R+b(read+write)
a+b(read+write)

Part-B
1.write any four differences between structure and union
S. No Structure Union

1. A structure is a collection of A union is also a collection of


number of data type may or may variables of different data types
not be of the same type grouped which may be basic data type or
together variable of another union.

2. Every member has its own memory All the members use the same
space. memory space to store the values.
3. Keyword struct is used. Keyword union is used.

4. All members can be accessed at a Only one member of a union and


time. thus one data type can be
accessed at any one time.
2.Explain how to access structure members
Ans. Accessing any member of the structure, we use dot (.) operator.
Which is also known as member operator or period operator? To access a particular
member, the dot operator must be placed between the name of the structure
variable and the name of the structure member.
The syntax of accessing structure member is,
Structurevariable.member;
Where,
Structurevariable: Name of the structure variable
Member: Name of a member within the structure
• student’s name of the structure “student” can be accessed by writing, stdl.name•

3.Explain the procedure to declare and initialize structure variables?


Ans. Structure declaration:The variable of a structure may also be declared
separately by using the structure Tag.
Example:
struct book_bank {
char title [20];
char author [15];
int pages;
float price;
};
struct book_bank b1, b2, b3;
• Here b1, b2 and b3 are three structure variables declared by the help of structure
tag i.e., struct book_bank.
Structure Initialization:
struct student {
char name[20];
int age;
};
struct book_bank b1= {“kalyan”,20};

4.write a c code to demonstrate array of structures.


Ans. #include <stdio.h>
struct Student {
char name[20];
int age;
};
int main() {
struct Student s[3];
int i;
printf(“Enter records:\n”);
for (i = 0; i < 3; i++) {
scanf(“%s %d”, s[i].name, &s[i].age);
}
printf(“\nName age\n ------- ------- “);
for (i = 0; i < 3; i++) {
printf(“\n %s %d”, s[i].name, s[i].age);
}
return 0;
}Output:
Enter records:
sham 20
mona 21
mani 23
Name age
------- -------
sham 20
mona 21
mani 23

5.what is nested structure, write a syntax with example.


Ans. when a structure is placed within another structure, as its member, then the
resultant structure is called nested structure.
Syntax:
struct name_1
{ member1;
member2;
.
.
membern;
struct name_2
{
member_1;
member_2;
.
.
member_n;
}, var1
} var2;
Example: The REMINDER structure declared in this method is shown in the
following C
Snippet.
//structure decalrations
struct DATE
{
int month;
int day;
int year;
}
struct TIME
{
int hour;
int min;
int sec;
struct REMINDER
{
struct DATE date;
struct TIME time;
//variable declaration
struct REMINDER reminder;
6. write about the functions used for file handling in c.
Ans.A file is a complex data type that is stored externally to the main memory,
usually on disk. In other words we can say, it is a storage area of memory, where
the data are stored in an organized form.
Fopen: File is opened by the function fopen().
Syntax: FILE *fp;
Fp=fopen(“file_name” mode);
Fclose(): It is closed a specified file.
Syntax: Fclose (FILE *);
fwrite() :This function is used for writing a structure variable in a file.
Syntax: Fwtite (const void *ptr, sizeof (data), no-of-data, FILE *fp);
fread():It is used to read an entire block from a given file.
Syntax: Fread (void *ptr, sizeof(data) no-of-data, FILE *fp);
fseek():It is used to move the file pointer in a particular position.
Syntax: fseek (FILE *fp, long size, mode);
ftell():It returns the current position of file pointer.
Syntax: long ftell (FILE *fp);
fprintf():It is same as printf() but it writes formatted data into the file.
fscanf():This function reads character, strings, integers, float etc., from the file
pointed by file pointer.

Part-C
1.Write a C program to implement nested structure.
#include <stdio.h>
// Define sub-structures
struct Address {
char street[50];
char city[50];
char state[20];
int zip;
};
struct DateOfBirth {
int day;
int month;
int year;
};
// Define high-level structure containing nested structures
struct Person {
char name[50];
int age;
struct Address address;
struct DateOfBirth dob;
};
int main() {
// Declare and initialize a person
struct Person person1 = {
.name = "John Doe",
.age = 30,
.address = {"123 Main St", "Anytown", "CA", 12345},
.dob = {10, 5, 1994}
};
// Print person's information
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Address: %s, %s, %s, %d\n", person1.address.street,
person1.address.city, person1.address.state, person1.address.zip);
printf("Date of Birth: %d/%d/%d\n", person1.dob.month, person1.dob.day,
person1.dob.year);
return 0;
}Output:
Name: John Doe
Age: 30
Address: 123 Main St, Anytown, CA, 12345
Date of Birth: 5/10/1994

2.Write a c program to implement array of structure.


#include <stdio.h>
struct Student {
char name[20];
int age;
};
int main() {
struct Student s[3];
int i;
printf("Enter records:\n");
for (i = 0; i < 3; i++) {
scanf("%s %d", s[i].name, &s[i].age);
}
printf("\nName age\n ------- ------- ");
for (i = 0; i < 3; i++) {
printf("\n %s %d", s[i].name, s[i].age);
}
return 0;
}Output:
Enter records:
sham 20
mona 21
mani 23
Name age
------- -------
sham 20
mona 21
mani 23

3.write a c program to demonstrate structure definition, declaration


accessing structure members.
#include <stdio.h>
struct library {
char name[30];
int price;
int pages;
};
int main() {
struct library book1 = {“Harrypotter”, 560, 1015};
struct library book2;
book2 = book1;
printf(“\nName: %s”, book2.name);
printf(“\nPrice: %d”, book2.price);
printf(“\nPages: %d”, book2.pages);
return 0;
}Output:
Name: Harrypotter
Price: 200
Pages: 1015

4.Explain about the nested structure concept with example program.


Ans. when a structure is placed within another structure, as its member, then the
resultant structure is called nested structure.
#include <stdio.h>
// Define sub-structures
struct Address {
char street[50];
char city[50];
char state[20];
int zip;
};
struct DateOfBirth {
int day;
int month;
int year;
};
// Define high-level structure containing nested structures
struct Person {
char name[50];
int age;
struct Address address;
struct DateOfBirth dob;
};
int main() {
// Declare and initialize a person
struct Person person1 = {
.name = "John Doe",
.age = 30,
.address = {"123 Main St", "Anytown", "CA", 12345},
.dob = {10, 5, 1994}
};
// Print person's information
printf("Name: %s\n", person1.name);
printf("Age: %d\n", person1.age);
printf("Address: %s, %s, %s, %d\n", person1.address.street,
person1.address.city, person1.address.state, person1.address.zip);
printf("Date of Birth: %d/%d/%d\n", person1.dob.month, person1.dob.day,
person1.dob.year);
return 0;
}Output:
Name: John Doe
Age: 30
Address: 123 Main St, Anytown, CA, 12345
Date of Birth: 5/10/1994

5.Explain about accessing structure members and structure assignment with


an example program.
Ans. Accessing structure members
For accessing any member of the structure, we use dot (.) operator.
Which is also known as member operator or period operator? To access a particular
member, the dot operator must be placed between the name of the structure
variable and the name of the structure member.
The syntax of accessing structure member is,
Structurevariable.member;
Where,
Structurevariable: Name of the structure variable
Member: Name of a member within the structure
• student’s name of the structure “student” can be accessed by writing, stdl.name.
Structure Assignment
• The one structure information can be assigned to another structure of the same
type using single assignment statement. We do not require to assign the value of
each member separatly.
• The syntax for structure assignment is,
Varl=var2;
Where, varl and var2 are the structure variables of same structure.
• Example: Consider the following statement.
Library bookl, book2;
Bookl-book2;
• Here, the value contained in book2 will be assigned in bookl.
Example program:
#include <stdio.h>
struct library {
char name[30];
int price;
int pages;
};
int main() {
struct library book1 = {"Harrypotter", 560, 1015};
struct library book2;
book2 = book1;
printf("\nName: %s", book2.name);
printf("\nPrice: %d", book2.price);
printf("\nPages: %d", book2.pages);
return 0;
}Output:
Name: Harrypotter
Price: 200
Pages: 1015
6.Explain Random handling functions.
Ans. random handling functions are
1. fseek()2. ftell()
3. rewind()
1.fseek():It is used to move the file pointer in a particular position.
Syntax: fseek (FILE *fp, long size, mode);
We can pass three arguments through the function.
1. File pointer
2. Positive long integer number to reposition the file pointer towards the backward
or forward direction.
3. Current position of file pointer (mode).
Mode
SEEK SET O - Beginning of file
SEEK CUR 1- Current position
SEEK END 2-End of the file
Example:
fseek ( f_{P}, 10 ,SEEK\ SET) 7
It skipped 10 byte forward from beginning of the file.
fseek ( fp, - 10 , SEEK END); It skipped 10 bytes forward from end of the file.
fseek (fp, 5, SEEK-CUR);
It skipped 5 bytes forward from the current position of the file.
2.ftell():It returns the current position of file pointer.
Syntax: long ftell (FILE *fp);
Example: long currentPosition = ftell(filePointer);
3.rewind():It is used to move the file pointer position to the beginning of the file.
Syntax: void rewind (FILE *fp);
Example: rewind(filePointer);

7.Write a c program to create a text file and print contents of the file in
reverse order using fseek ()and ftell ()functions.
#include <stdio.h>
#include <stdlib.h> // For exit()
int main() {
FILE *fptr;
char filename[100], c;
printf("Enter the filename to open: ");
scanf("%s", filename);
// Open file for reading
fptr = fopen(filename, "r");
if (fptr == NULL) {
printf("Cannot open file.\n");
exit(0);
}// Read contents from file
c = fgetc(fptr);
while (c != EOF) {
printf("%c", c);
c = fgetc(fptr);
}
fclose(fptr);
return 0;}

You might also like