You are on page 1of 11

1. Write a program to find the length of a string using pointers.

#include <stdio.h>
int main() {
char a[20];
int count = 0;
char *p;

printf("Enter the string: ");


scanf("%s", a);
p = a;
while (*p != '\0')
{
count++;
p++;
}
printf("The length of string is %d\n", count);
return 0;
}
2. Explain the use of typedef with structure in c. provide an
example.

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);

printf("the sum of 2 complex number is


%d+%di\n",(c1.real+c2.real),(c1.ima+c2.ima));

printf("the subtraction of 2 complex number is %d+%di\n",(c1.real-


c2.real),(c1.ima-c2.ima));
return 0;
}

3. Explain the concept of nested structures with an example.

A structure inside another structure is called nested structure . this


concept alloys to create more complex data structure by combining
multiple structures into one. Nested structures enables to represent
hierarchical relationships between data elements.
Example :

#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);

printf("Enter the birthdate (year month day): ");


scanf("%d %d %d", &stu.dob.year, &stu.dob.month, &stu.dob.day);

printf("The name of student is %s\n", stu.name);


printf("Date of birth of student is %d/%d/%d\n", stu.dob.year,
stu.dob.month, stu.dob.day);

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");
}

void searchbook(int n,char a[])


{
int found = 0;
for(int i=0;i<=total_1;i++)
{
if(strcmp(library[i].title,a)==0)
{
printf("Your book is found: ");
printf("title :- %s\n",library[i].title);
printf("author:- %s\n",library[i].author);
printf("ISBN :- %d\n",library[i].ISBN);
found= 1;
break;
}

}
if(found == 0)
{
printf("You book is not found.");
}
}
5. What is the difference between call by value and call by
reference? Provide examples.

The value of variables that Is directly pass to a function is called call by


value. It copies the value of passed parameter into the function’s
arguments. Thus, any changes that occur in any argument insider the
function have no reflection on the passed parameter. So there is no
modification in the original value.
Whenever calling a function rather passing the variables values, pass its
address instead to the function is called call by reference. In this method
both passed parameter and argument refer to a similar location. Thus any
change occurring In any argument inside the function also reflects in the
passed parameter. So there is a modification in the original value.

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.

Define the structure: This involves declaring a structure that represents


the data you want to read from or write to the binary file.

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.

Read from or write to the file:

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);
}
}

8. A pointer variable is used to store address of some other


variables, however, we need to specify data type while
declaring a pointer variable. Why?

In C, pointer variables must have a specified data type because pointers


are associated with a particular type of data they point to. When we
declare a pointer variable, we inform the compiler to reserve memory to
hold an address. However, the size of the memory allocated for the
pointer depends on the data type it points to. Therefore, if we don't
specify the data type for the pointer variables, the compiler wouldn't
know how to properly allocate memory, perform pointer arithmetic, or
dereference the pointers. Hence, specifying the data type is essential for
correct memory management and proper operation of the pointers.
Name : Kushal Bhattarai
Roll no: 080BEL038
Group : B

You might also like