Compute Assisment 2
Compute Assisment 2
Compute Assisment 2
#include <stdio.h>
int main() {
char a[20];
int count = 0;
char *p;
In C, the typedef keyword enables users to create aliases for existing data
types, providing the flexibility to use a new name instead of the original
data type name. This enhances code readability, especially when dealing
with lengthy structure names or complex data structures. By creating
shorter, more descriptive names, typedef improves code organization and
comprehension. Typedef can abstract implementation details, offering a
layer of separation and simplifying code maintenance. So that typedef is
commonly used in structures to enhance the readability and
manageability of code.
Example:
#include<stdio.h>
#include<conio.h>
typedef struct
{
int real;
int ima;
}complex;
int main()
{
complex c1,c2;
printf("enter the first complex number: ");
scanf("%d %d",&c1.real,&c1.ima);
printf("enter the second complex number: ");
scanf("%d %d",&c2.real,&c2.ima);
#include<stdio.h>
struct date {
int year, month, day;
};
struct student {
char name[50];
struct date dob;
} stu;
int main() {
printf("Enter the name of student: ");
scanf("%s", stu.name);
return 0;
}
4. Create an array of structures to store information about
books (title, author, ISBN). Write a function to search for a
book by its title.
#include<stdio.h>
#include<string.h>
struct book
{
char title[50];
char author[100];
int ISBN;
};
struct book library[100];
void storeinformation( int );
void searchbook(int, char[]);
void read_first();
FILE *fp;
int total;
int total_1;
void read_first()
{
total = 0;
while (fread(&library[total],154,1,fp))
{
total++;
}
fclose(fp);
total_1 = total;
total=0;
}
void main()
{
fp=fopen("library.txt","r");
read_first();
int n,i;
char a[50];
int choice;
printf("What do you want to do add new or view a book.(1/2)");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter the number of book: ");
scanf("%d",&n);
storeinformation(n);
break;
case 2:
printf("\n\enter the book title you want to search\n");
scanf("%s",&a);
searchbook(n,a);
break;
}
}
void storeinformation(int n)
{
printf("Enter the information about books \n ");
for(int i=0;i<n;i++)
{
printf("enter the title of book: ");
scanf("%s",&library[i].title);
printf("enter the author of book: ");
scanf("%s",&library[i].author);
printf("enter the ISBN : ");
scanf("%d",&library[i].ISBN);
total ++;
}
fp = fopen("library.txt","a");
for ( int j = 0;j<total;j++)
{
fwrite(&library[j],1,154,fp);
}
printf("information stored successfully:\n\n");
}
}
if(found == 0)
{
printf("You book is not found.");
}
}
5. What is the difference between call by value and call by
reference? Provide examples.
Example:
#include<stdio.h>
int value1(int);
int value2(int*);
void main()
{
int p;
int *q=&p;
printf("enter the value: ");
scanf("%d",&p);
int result1=value1(p);
int result2=value2(q);
printf("the value change after call by value is %d\n",result1);
printf("the value change after call by reference is %d\n",result2);
}
int value1(int p)
{
return p+5;
}
int value2(int *q)
{
return (*q)+6;
}
6. Write a program to read data from a text file and display it on
the console.
#include<stdio.h>
void main()
{
FILE *fp;
char name[200];
fp=fopen("student.txt","w");
if (fp == NULL) {
printf("Error opening file for writing.\n");
return 1;
}
printf("enter the name of student : ");
gets(name);
fprintf(fp,"name is %s\n\n",name);
fclose(fp);
fp= fopen("student.txt","r");
char ch;
ch=getc(fp);
while(ch!=EOF)
{
printf("%c",ch);
ch=getc(fp);
}
fclose(fp);
return 0; }
7. How can you read and write a structure to a binary file in C?
Provide an example.
Open the binary file: Use the fopen() function to open the binary file in
the appropriate mode ("rb" for reading or "wb" for writing). Make sure to
check if the file was opened successfully.
a. For reading: Use the fread() function to read data from the binary
file into the structure. You need to provide the address of the
structure, the size of each element, the number of elements, and
the file pointer.
b. For writing: Use the fwrite() function to write data from the
structure to the binary file. Similarly, you need to provide the
address of the structure, the size of each element, the number of
elements, and the file pointer.
Close the file: After reading from or writing to the file, use the fclose()
function to close the file. This is important to release any resources
associated with the file.
Example:
#include<stdio.h>
struct student
{
char name[50];
int roll;
int marks;
}stu1;
void main()
{
FILE *fp;
printf("enter the name of stuent : \n");
scanf("%s",&stu1.name);
printf("enter the roll number of student: \n");
scanf("%d",&stu1.roll);
printf("enter the marks of studen: \n");
scanf("%d",&stu1.marks);
fp=fopen("student1.txt","wb");
if (fp == NULL)
{
printf("Error opening file for writing.\n");
return 1;
}
fwrite(&stu1,1,sizeof(struct student),fp);
fclose(fp);
fp=fopen("student1.txt","r");
fread(&stu1,1,sizeof(struct student),fp);
{
printf("name is \n%s",stu1.name);
printf("marks is \n%d",stu1.marks);
printf("roll is \n%d",stu1.roll);
}
}