Naina Miss Homework
Naina Miss Homework
int length = 0;
while (*str != '\0') {
length++;
str++;
}
return length;
}
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str);
int length = len(str);
printf("Length of the string: %d\n", length);
return 0;
}
2. Explain the use of typedef with structures in C. Provide an example.
In C programming, typedef is used to create an alias or alternative name for existing data
types, including user-defined data types such as structures. When used with structures,
typedef allows you to define a new type name for a structure, making the code more readable
and easier to maintain.
#include <stdio.h>
typedef struct {
int x;
int y;
} Point;
int main() {
Point p1;
p1.x = 5;
p1.y = 10;
printf("Coordinates of p1: (%d, %d)\n", p1.x, p1.y);
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[100];
char author[100];
char ISBN[100];
};
int searchBookByTitle(struct Book books[], int numBooks, const char *title) {
int i = 0;
for (i = 0; i < numBooks; i++) {
if (strcmp(books[i].title, title) == 0) {
return i; }
}
return -1; }
int main() {
struct Book library[100] = {
{"Theory Of Everything", "Stephen Hawking", "9780743273565"},
{"On Heat", "James Clerk Maxwell", "9780061120084"},
{"1984", "George Orwell", "9780451524935"},
{“Concept of C”,”Nainya Amatya”,”9867647549”}
};
int numBooks = 4;
char searchTitle[100];
printf("Enter the title of the book to search for: ");
scanf(“%s”,&searchTitle);
int index = searchBookByTitle(library, numBooks, searchTitle);
if (index != -1) {
printf("Book found:\n");
printf("Title: %s\n", library[index].title);
printf("Author: %s\n", library[index].author);
printf("ISBN: %s\n", library[index].ISBN);
} else {
printf("Book not found.\n");
}
return 0;
}
5. What is the difference between call by value and call by reference? Provide examples
Call by Value:
- In call by value, a copy of the actual parameter's value is passed to the function.
- Any modifications made to the parameter inside the function do not affect the original
argument.
- It is used for passing primitive data types such as integers, floats, characters, etc.
- Changes made to the parameter inside the function are not reflected outside the
function.
EG:
#include <stdio.h>
void increment(int x) {
x++;}
int main() {
int num = 5;
printf("Before function call: %d\n", num);
increment(num);
printf("After function call: %d\n", num);
return 0;
}
Call by Reference:
- In call by reference, the memory address (reference) of the actual parameter is passed
to the function.
- Any modifications made to the parameter inside the function directly affect the original
argument.
- It is used for passing complex data types such as arrays, structures, and pointers.
- Changes made to the parameter inside the function are reflected outside the function.
EG:
#include <stdio.h>
(*x)++;
int main() {
int num = 5;
increment(&num);
return 0;
6. Write a program to read data from a text file and display it on the console.
#include <stdio.h>
int main() {
FILE *file;
char filename[100];
char ch;
printf("Enter the filename: ");
scanf("%s", filename);
file = fopen(filename, "r");
if (file == NULL) {
printf("Error opening the file.\n");
return 1;
}
printf("Contents of the file:\n");
while ((ch = fgetc(file)) != EOF) {
putchar(ch);
}
fclose(file);
return 0;
}
7. How can you read and write a structure to a binary file in C? Provide an example.
In C, you can read and write a structure to a binary file using functions like fwrite() and
fread(). Here's an example demonstrating how to do thisIn C, you can read and write a
structure to a binary file using functions like fwrite() and fread().
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Book {
char title[100];
char author[100];
};
int main() {
if (fileWrite == NULL) {
return 1;
fclose(fileWrite);
if (fileRead == NULL) {
return 1;
fclose(fileRead);
printf("Title: %s\n", readBook.title);
return 0;
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?
When you're working with pointers in C, it's super important to tell the computer what kind
of data the pointer will be pointing to. This helps the computer know how much space it
needs to set aside in memory for that pointer. Also, it helps with moving around in memory
correctly. Like, if you're counting steps, you need to know how big each step is, right? That's
kind of what specifying the data type does for pointers. And when you want to look at
what's actually stored at the memory location the pointer points to, specifying the data type
helps the compiler understand what it's looking at. Plus, it helps catch mistakes early on.
Like, if you accidentally try to mix up different types of data, the computer can warn you
about it before you run your program. So, in simple words, telling the computer what type of
data your pointer will be pointing to is like giving it a map to navigate through memory
safely.
Meyan Adhikari
Thank you