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

UNIT-5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

UNIT-5

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

UNIT-5

1. Dynamic Memory Allocation Functions

Dynamic memory allocation allows you to allocate memory during


runtime using functions such as malloc(), calloc(), realloc(), and free().

malloc(): Allocates memory of a specified size.

calloc(): Allocates memory for an array and initializes it to zero.

realloc(): Resizes a previously allocated memory block.

free(): Releases allocated memory.

Example: Using malloc()

#include <stdio.h>
#include <stdlib.h>

int main() {
int *ptr = malloc(4 * sizeof(int)); // Allocate memory for 4 integers
ptr[0] = 10; // Assign value
printf("%d\n", ptr[0]); // Output: 10

free(ptr); // Free memory


return 0;
}

---

2. Significance of malloc()

malloc() allocates a block of memory and returns a pointer to the first


byte of the block. It doesn’t initialize the memory, meaning it may
contain garbage values.

Example:

#include <stdio.h>
#include <stdlib.h>

int main() {
int *ptr = malloc(5 * sizeof(int)); // Allocate memory for 5 integers
ptr[0] = 10; // Assign value
printf("%d\n", ptr[0]); // Output: 10

free(ptr); // Free memory


return 0;
}

---

3. Significance of realloc()

realloc() is used to resize a previously allocated memory block. It can


increase or decrease the size of the memory block.

Example:

#include <stdio.h>
#include <stdlib.h>

int main() {
int *ptr = malloc(3 * sizeof(int)); // Allocate memory for 3 integers

ptr = realloc(ptr, 5 * sizeof(int)); // Resize memory to 5 integers


ptr[0] = 20; // Assign value
printf("%d\n", ptr[0]); // Output: 20

free(ptr); // Free memory


return 0;
}

---

4. Largest Element Using Dynamic Memory Allocation

You can find the largest element by dynamically allocating memory


and storing the values in an array.

Example:

#include <stdio.h>
#include <stdlib.h>

int main() {
int *arr = malloc(5 * sizeof(int)); // Allocate memory for 5 integers
arr[0] = 2, arr[1] = 4, arr[2] = 1, arr[3] = 8, arr[4] = 6;

int largest = arr[0];


for(int i = 1; i < 5; i++) {
if(arr[i] > largest) largest = arr[i];
}

printf("Largest: %d\n", largest); // Output: 8

free(arr); // Free memory


return 0;
}

---

5. Structure Definition and Initialization

Structures in C allow grouping variables of different types together.


You can define and initialize a structure with different members.

Example:

#include <stdio.h>

struct Person {
char name[50];
int age;
};

int main() {
struct Person p = {"John", 25}; // Initialize structure

printf("%s %d\n", p.name, p.age); // Output: John 25


return 0;
}

---

6. Accept Name, Age of a Person and Display

You can define a structure to hold the details of a person, accept


input, and display it.

Example:

#include <stdio.h>

struct Person {
char name[50];
int age;
};
int main() {
struct Person p;

printf("Name: ");
fgets(p.name, sizeof(p.name), stdin); // Accept name
printf("Age: ");
scanf("%d", &p.age); // Accept age

printf("%s %d\n", p.name, p.age); // Display details


return 0;
}

---

7. Accept and Display Student Details Using Structures

Here we define a structure for student details, accept input for


multiple students, and display the details.

Example:

#include <stdio.h>
struct Student {
char name[50];
int rollNo;
};

int main() {
struct Student s[5];

for(int i = 0; i < 5; i++) {


printf("Enter name: ");
fgets(s[i].name, sizeof(s[i].name), stdin); // Accept name
printf("Enter roll number: ");
scanf("%d", &s[i].rollNo); // Accept roll number
getchar(); // To clear newline character
}

for(int i = 0; i < 5; i++) {


printf("%s %d\n", s[i].name, s[i].rollNo); // Display student details
}

return 0;
}

---
8. Differences Between Structure and Union

Structure: Each member has its own memory location. All members
can hold values simultaneously.

Union: All members share the same memory location. Only one
member can hold a value at any given time.

---

9. What is a File and File Operations in C

A file is a collection of data stored on a storage device. You can


perform various operations like opening, closing, reading, and writing
files using functions like fopen(), fclose(), fscanf(), fprintf().

---

10. File Access Modes in C

File access modes specify how a file should be opened. Some


common modes are:

r: Open for reading.

w: Open for writing (creates or overwrites a file).

a: Open for appending.

r+: Open for both reading and writing.

w+: Open for both reading and writing (overwrites).

---

11. Write Text into a File Using File Handling Functions

You can open a file in write mode and write text into it using fprintf().

Example:

#include <stdio.h>

int main() {
FILE *file = fopen("test.txt", "w"); // Open file in write mode
fprintf(file, "Hello, World!\n"); // Write text to file
fclose(file); // Close the file

return 0;
}

You might also like