1. Dynamic Memory Allocation
Dynamic Memory Allocation is a process in C where memory is allocated during runtime (i.e., while the program is running), rather than at compile time.
It allows you to allocate memory at runtime using functions from <stdlib.h>.
Functions:
| Function | Purpose |
|---|---|
malloc() | Allocates a block of memory |
calloc() | Allocates & initializes memory |
free() | Frees previously allocated memory |
📘 Example using malloc:
cCopyEdit#include <stdio.h>
#include <stdlib.h>
int main() {
int *ptr;
ptr = (int*) malloc(5 * sizeof(int));
if (ptr == NULL) {
printf("Memory not allocated.\n");
return 1;
}
for(int i = 0; i < 5; i++) {
ptr[i] = i + 1;
printf("%d ", ptr[i]);
}
free(ptr); // Deallocating memory
return 0;
}
2. 🔁 Advanced Pointers
✅ Pointer to Pointer:
It stores the address of another pointer.
cCopyEditint a = 10;
int *p = &a;
int **pp = &p;
printf("%d\n", **pp); // Output: 10
✅ Function Pointers:
Stores address of a function. Useful in callbacks and implementing plug-in behavior.
📘 Example:
cCopyEdit#include <stdio.h>
void greet() {
printf("Hello from function pointer!\n");
}
int main() {
void (*fp)() = greet;
fp(); // Call function using pointer
return 0;
}
3. 📂 File Handling in C
File I/O allows reading and writing to files.
✅ Common functions:
fopen(),fclose()fprintf(),fscanf()fread(),fwrite()fgets(),fputs()
📘 Example: Write and read from a file
cCopyEdit#include <stdio.h>
int main() {
FILE *fp;
fp = fopen("data.txt", "w");
fprintf(fp, "Welcome, Keshab!");
fclose(fp);
char content[50];
fp = fopen("data.txt", "r");
fgets(content, 50, fp);
printf("File content: %s\n", content);
fclose(fp);
return 0;
}
4. ⚙️ Bitwise Operators
Used for bit-level operations (speedy and memory-efficient).
✅ Types:
| Operator | Name | Example Result |
|---|---|---|
& | AND | 5 & 3 = 1 |
| ` | ` | OR |
^ | XOR | 5 ^ 3 = 6 |
~ | NOT | ~5 = -6 |
<< | Left shift | 5 << 1 = 10 |
>> | Right shift | 5 >> 1 = 2 |
📘 Example:
cCopyEditint a = 5, b = 3;
printf("AND: %d\n", a & b);
printf("OR: %d\n", a | b);
5. 🧱 Linked Lists, Stacks, and Queues
✅ Linked List:
A list of dynamically connected nodes using pointers.
📘 Example: Singly Linked List
cCopyEditstruct Node {
int data;
struct Node *next;
};
✅ Stack (LIFO):
- Push, Pop
- Implement using arrays or linked lists
✅ Queue (FIFO):
- Enqueue, Dequeue
- Also using arrays or linked lists
📘 Stack with array:
cCopyEdit#define SIZE 5
int stack[SIZE], top = -1;
void push(int val) {
if(top < SIZE - 1) stack[++top] = val;
}
int pop() {
if(top >= 0) return stack[top--];
}
6. 🧩 Complex Programs
📘 1. File-Based Student Record System
cCopyEdittypedef struct {
int id;
char name[30];
float marks;
} Student;
void writeToFile() {
FILE *fp = fopen("students.dat", "wb");
Student s = {101, "Keshab", 93.5};
fwrite(&s, sizeof(Student), 1, fp);
fclose(fp);
}
void readFromFile() {
FILE *fp = fopen("students.dat", "rb");
Student s;
fread(&s, sizeof(Student), 1, fp);
printf("ID: %d, Name: %s, Marks: %.2f\n", s.id, s.name, s.marks);
fclose(fp);
}
📘 2. Linked List-based Student Database
- Add, Delete, Search, Update student data
- Use
malloc()to create nodes dynamically
✅ Summary Table
| Topic | Description | Example Function or Use |
|---|---|---|
| Dynamic Memory Allocation | Allocate/free memory at runtime | malloc(), free() |
| Advanced Pointers | Pointer to pointer, function pointers | void (*fp)() |
| File Handling | Read/write files | fopen(), fread() |
| Bitwise Operators | Bit manipulation | a & b, a << 1 |
| Linked List / Stack/Queue | Data structures using pointers | struct Node |
| Complex Programs | File-based systems, DS implementation | Student DB System |