Unit5 Notes With Example
Unit5 Notes With Example
Unit5 Notes With Example
Dynamic memory allocation in C allows you to allocate memory at runtime, which can be very useful for
managing memory in situations where you do not know the exact amount of memory needed during
compile time. The standard library(stdlib.h) in C provides several functions for dynamic memory
allocation, including malloc(), calloc(), realloc(), and free(). Here's a brief overview of each:
malloc(): Allocates a specified number of bytes and returns a pointer to the allocated memory. The
memory is not initialized.
#include <stdlib.h>
int *arr = (int *)malloc(10 * sizeof(int));
if (arr == NULL) {
// handle memory allocation failure
}
calloc(): Allocates memory for an array of elements, initializes all bytes to zero, and returns a pointer to
the allocated memory.
#include <stdlib.h>
int *arr = (int *)calloc(10, sizeof(int));
if (arr == NULL) {
// handle memory allocation failure
}
realloc(): Resizes the memory block pointed to by a pointer to a new size. If the new size is larger, the
additional memory is uninitialized.
#include <stdlib.h>
int *arr = (int *)malloc(10 * sizeof(int));
if (arr == NULL) {
// handle memory allocation failure
}
arr = (int *)realloc(arr, 20 * sizeof(int));
if (arr == NULL) {
// handle memory allocation failure
}
Example:
malloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
int *arr;
return 0;
}
calloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int n, i;
int *arr;
return 0;
}
realloc()
#include <stdio.h>
#include <stdlib.h>
int main() {
int n = 5; // initial number of elements
int new_n = 10; // new number of elements
int i;
int *arr;
return 0;
}
File handling in C
File handling in C allows you to create, open, read, write, and close files. The standard library provides
several functions for file operations, which are declared in the <stdio.h> header file.
Opening a File:
Closing a File:
Ex:
#include <stdio.h>
int main() {
FILE *sourceFile, *destFile;
char ch;
return 0;
}
Storage Class in C
In C, storage classes define the scope (visibility) and lifetime of variables and/or functions within
a program. There are four storage classes in C:
#include <stdio.h>
void function() {
auto int x = 10; // auto keyword is optional
printf("Auto variable x = %d\n", x);
}
int main() {
function();
return 0;
}
#include <stdio.h>
int main() {
register int x = 10; // suggest storing x in a register
printf("Register variable x = %d\n", x);
return 0;
}
• Scope: Local to the block in which it is defined (for local variables) or file scope (for
global variables).
• Lifetime: Exists for the entire duration of the program.
• Usage: Preserves the value of the variable between function calls (for local variables) or
restricts the visibility to the file (for global variables).
#include <stdio.h>
void function() {
static int x = 0; // static variable retains its value between calls
x++;
printf("Static variable x = %d\n", x);
}
int main() {
function(); // Output: 1
function(); // Output: 2
function(); // Output: 3
return 0;
}
File1.c:
#include <stdio.h>
void print_x() {
printf("x = %d\n", x);
}
File2.c:
#include <stdio.h>
int main() {
print_x(); // Output: x = 10
return 0;
}