// PRACTICAL 1
// 1. Resize 1D array (increase/decrease)
#include <stdio.h>
#include <stdlib.h>
int main() {
int *arr = (int *)malloc(3 * sizeof(int));
arr[0] = 10; arr[1] = 20; arr[2] = 30;
arr = (int *)realloc(arr, 5 * sizeof(int));
arr[3] = 40; arr[4] = 50;
for (int i = 0; i < 5; i++) prin ("%d ", arr[i]);
free(arr);
return 0;
/*
Expected Output:
10 20 30 40 50
*/
// 2. Resize 2D array
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows = 2, cols = 2;
int **matrix = malloc(rows * sizeof(int *));
for (int i = 0; i < rows; i++)
matrix[i] = malloc(cols * sizeof(int));
matrix[0][0] = 1; matrix[0][1] = 2;
matrix[1][0] = 3; matrix[1][1] = 4;
// Increase rows
matrix = realloc(matrix, 3 * sizeof(int *));
matrix[2] = malloc(cols * sizeof(int));
matrix[2][0] = 5; matrix[2][1] = 6;
// Print new matrix
for (int i = 0; i < 3; i++) {
for (int j = 0; j < cols; j++)
prin ("%d ", matrix[i][j]);
prin ("\n");
for (int i = 0; i < 3; i++)
free(matrix[i]);
free(matrix);
return 0;
/*
Expected Output:
12
34
56
*/
// PRACTICAL 2
// 1. Compare malloc vs calloc
#include <stdio.h>
#include <stdlib.h>
int main() {
int *a = malloc(5 * sizeof(int));
int *b = calloc(5, sizeof(int));
prin ("malloc:
");
for (int i = 0; i < 5; i++) prin ("%d ", a[i]);
prin ("\ncalloc:
");
for (int i = 0; i < 5; i++) prin ("%d ", b[i]);
free(a); free(b);
return 0;
/*
Expected Output:
malloc:
<garbage values>
calloc:
00000
*/
// 2. Dynamically allocate string
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main() {
char *str = malloc(50);
strcpy(str, "Hello Dynamic Memory!");
prin ("%s\n", str);
free(str);
return 0;
/*
Expected Output:
Hello Dynamic Memory!
*/
// PRACTICAL 3
// 1. Access variable using pointer
#include <stdio.h>
int main() {
int x = 10, *p = &x;
prin ("Value = %d\n", *p);
return 0;
/*
Expected Output:
Value = 10
*/
// 2. Swap using pointers
#include <stdio.h>
void swap(int *a, int *b) {
int temp = *a; *a = *b; *b = temp;
int main() {
int x = 5, y = 10;
swap(&x, &y);
prin ("x = %d, y = %d\n", x, y);
return 0;
/*
Expected Output:
x = 10, y = 5
*/
// 3. Access array using pointer
#include <stdio.h>
int main() {
int arr[] = {1, 2, 3, 4};
int *p = arr;
for (int i = 0; i < 4; i++) prin ("%d ", *(p + i));
return 0;
}
/*
Expected Output:
1234
*/
// PRACTICAL 4
// [Already added in previous message]
// PRACTICAL 5
// [Already added in previous message]
// PRACTICAL 6
// 1. Enumera on for days of the week
#include <stdio.h>
enum Day {SUN, MON, TUE, WED, THU, FRI, SAT};
int main() {
enum Day today = WED;
prin ("Day number: %d
", today);
return 0;
/*
Expected Output:
Day number: 3
*/
// 2. Structure for employee details
#include <stdio.h>
struct Employee {
int id;
char name[20];
float salary;
};
int main() {
struct Employee emp[5] = {
{1, "Alice", 50000},
{2, "Bob", 45000},
{3, "Charlie", 47000},
{4, "David", 52000},
{5, "Eve", 49000}
};
for(int i = 0; i < 5; i++)
prin ("%d %s %.2f\n", emp[i].id, emp[i].name, emp[i].salary);
return 0;
/*
Expected Output:
1 Alice 50000.00
2 Bob 45000.00
3 Charlie 47000.00
4 David 52000.00
5 Eve 49000.00
*/
// 3. Nested structure for student's academic record
#include <stdio.h>
struct Personal {
char name[20];
int age;
char address[50];
};
struct Academic {
struct Personal p;
float marks;
};
int main() {
struct Academic a = {{"John", 20, "123 Main St"}, 85.5};
prin ("%s %d %s %.1f\n", a.p.name, a.p.age, a.p.address, a.marks);
return 0;
/*
Expected Output:
John 20 123 Main St 85.5
*/
// PRACTICAL 7
// 1. Pass structure to func on
#include <stdio.h>
struct Pair {
int a, b;
};
int sum(struct Pair p) {
return p.a + p.b;
int main() {
struct Pair p = {5, 10};
prin ("Sum = %d\n", sum(p));
return 0;
/*
Expected Output:
Sum = 15
*/
// 2. Union example
#include <stdio.h>
union Data {
int i;
float f;
char ch;
};
int main() {
union Data d;
d.i = 10;
prin ("Integer: %d\n", d.i);
d.f = 3.14;
prin ("Float: %.2f\n", d.f);
d.ch = 'A';
prin ("Char: %c\n", d.ch);
return 0;
/*
Expected Output:
Char: A (other values are overwri en due to union)
*/
// 3. Typedef with complex number
#include <stdio.h>
typedef struct {
int real, imag;
} Complex;
Complex add(Complex a, Complex b) {
Complex c;
c.real = a.real + b.real;
c.imag = a.imag + b.imag;
return c;
}
int main() {
Complex x = {3, 2}, y = {1, 7}, z;
z = add(x, y);
prin ("Sum: %d + %di\n", z.real, z.imag);
return 0;
/*
Expected Output:
Sum: 4 + 9i
*/
// PRACTICAL 8
// 1. Create and write to file
#include <stdio.h>
int main() {
FILE *f = fopen("file1.txt", "w");
fprin (f, "Hello File");
fclose(f);
return 0;
// 2. Read from file
#include <stdio.h>
int main() {
FILE *f = fopen("file1.txt", "r");
char ch;
while ((ch = fgetc(f)) != EOF)
putchar(ch);
fclose(f);
return 0;
/*
Expected Output:
Hello File
*/
// 3. Append to file
#include <stdio.h>
int main() {
FILE *f = fopen("file1.txt", "a");
fprin (f, "\nAppended Line");
fclose(f);
return 0;
// PRACTICAL 9
// 1. Search word in file
#include <stdio.h>
#include <string.h>
int main() {
FILE *f = fopen("file1.txt", "r");
char word[50], target[] = "Hello";
int count = 0;
while (fscanf(f, "%s", word) != EOF)
if (strcmp(word, target) == 0) count++;
prin ("'%s' found %d mes\n", target, count);
fclose(f);
return 0;
// 2. Reverse contents of file
#include <stdio.h>
int main() {
FILE *f1 = fopen("file1.txt", "r");
FILE *f2 = fopen("reversed.txt", "w");
fseek(f1, 0, SEEK_END);
long size = ell(f1);
for (long i = size - 1; i >= 0; i--) {
fseek(f1, i, SEEK_SET);
fputc(fgetc(f1), f2);
fclose(f1); fclose(f2);
return 0;
}
// 3. Read specific line from file
#include <stdio.h>
#include <string.h>
int main() {
FILE *f = fopen("file1.txt", "r");
char line[100];
int lineNo = 2, current = 1;
while (fgets(line, sizeof(line), f)) {
if (current == lineNo) {
prin ("Line %d: %s", lineNo, line);
break;
current++;
fclose(f);
return 0;
// PRACTICAL 10
// 1. Bubble Sort
#include <stdio.h>
int main() {
int a[] = {4, 2, 5, 3}, n = 4;
for(int i = 0; i < n-1; i++)
for(int j = 0; j < n-i-1; j++)
if(a[j] > a[j+1]) {
int t = a[j]; a[j] = a[j+1]; a[j+1] = t;
for(int i = 0; i < n; i++) prin ("%d ", a[i]);
return 0;
// 2. Inser on Sort
#include <stdio.h>
int main() {
int a[] = {5, 3, 4, 1}, n = 4;
for(int i = 1; i < n; i++) {
int key = a[i], j = i - 1;
while(j >= 0 && a[j] > key)
a[j + 1] = a[j--];
a[j + 1] = key;
for(int i = 0; i < n; i++) prin ("%d ", a[i]);
return 0;
// 3. Linear Search
#include <stdio.h>
int main() {
int a[] = {4, 2, 5, 1}, n = 4, key = 5;
for(int i = 0; i < n; i++)
if(a[i] == key) {
prin ("Found at index %d\n", i);
return 0;
prin ("Not found\n");
return 0;
// 4. Binary Search
#include <stdio.h>
int main() {
int a[] = {1, 2, 3, 4, 5}, n = 5, key = 3;
int l = 0, r = n - 1, mid;
while(l <= r) {
mid = (l + r) / 2;
if(a[mid] == key) {
prin ("Found at index %d\n", mid);
return 0;
} else if(key < a[mid])
r = mid - 1;
else
l = mid + 1;
prin ("Not found\n");
return 0;
}
// 5. Selec on Sort
#include <stdio.h>
int main() {
int a[] = {5, 2, 4, 3}, n = 4;
for(int i = 0; i < n-1; i++) {
int min = i;
for(int j = i+1; j < n; j++)
if(a[j] < a[min]) min = j;
int t = a[i]; a[i] = a[min]; a[min] = t;
for(int i = 0; i < n; i++) prin ("%d ", a[i]);
return 0;