Write a program to swap two numbers using pointers.
cCopyEdit#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);
printf("x = %d, y = %d\n", x, y);
return 0;
}
Explanation:
By passing addresses (&x, &y), the function can modify the original variables using pointers.
2. Write a program to create a structure for a student and display the details.
cCopyEdit#include <stdio.h>
struct Student {
int roll;
char name[50];
float marks;
};
int main() {
struct Student s1 = {1, "Keshab", 87.5};
printf("Roll: %d\n", s1.roll);
printf("Name: %s\n", s1.name);
printf("Marks: %.2f\n", s1.marks);
return 0;
}
Explanation:
Structures combine different data types. You can access members using dot . operator.
3. Write a program to reverse a string using pointers.
cCopyEdit#include <stdio.h>
#include <string.h>
int main() {
char str[] = "pointer";
char *start = str;
char *end = str + strlen(str) - 1;
char temp;
while (start < end) {
temp = *start;
*start = *end;
*end = temp;
start++;
end--;
}
printf("Reversed string: %s\n", str);
return 0;
}
Explanation:
Using two pointers (start and end), we swap characters moving inward.
4. Write a program to dynamically allocate memory for an array and input values.
cCopyEdit#include <stdio.h>
#include <stdlib.h>
int main() {
int n;
printf("Enter size of array: ");
scanf("%d", &n);
int *arr = (int*) malloc(n * sizeof(int));
if (arr == NULL) {
printf("Memory not allocated\n");
return 1;
}
printf("Enter elements:\n");
for (int i = 0; i < n; i++)
scanf("%d", &arr[i]);
printf("Entered array:\n");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
free(arr);
return 0;
}
Explanation:malloc allocates memory at runtime. free is used to release it.
5. Write a program to copy contents of one file to another.
cCopyEdit#include <stdio.h>
int main() {
FILE *src = fopen("input.txt", "r");
FILE *dest = fopen("output.txt", "w");
char ch;
if (src == NULL || dest == NULL) {
printf("Error opening file\n");
return 1;
}
while ((ch = fgetc(src)) != EOF)
fputc(ch, dest);
printf("File copied successfully.\n");
fclose(src);
fclose(dest);
return 0;
}
Explanation:
Read character-by-character using fgetc() and write using fputc().
6. Write a program to count number of lines, words, and characters in a file.
cCopyEdit#include <stdio.h>
#include <ctype.h>
int main() {
FILE *fp = fopen("input.txt", "r");
int ch, lines = 0, words = 0, chars = 0;
int in_word = 0;
if (fp == NULL) {
printf("File not found.\n");
return 1;
}
while ((ch = fgetc(fp)) != EOF) {
chars++;
if (ch == '\n') lines++;
if (isspace(ch))
in_word = 0;
else if (!in_word) {
in_word = 1;
words++;
}
}
fclose(fp);
printf("Lines: %d, Words: %d, Characters: %d\n", lines, words, chars);
return 0;
}
Explanation:
We count characters, words using a flag (in_word), and lines via newline characters.
7. Write a program to find the second largest number in an array.
cCopyEdit#include <stdio.h>
int main() {
int arr[] = {10, 25, 40, 15, 30}, n = 5;
int first = arr[0], second = -1;
for (int i = 1; i < n; i++) {
if (arr[i] > first) {
second = first;
first = arr[i];
} else if (arr[i] > second && arr[i] != first) {
second = arr[i];
}
}
printf("Second largest: %d\n", second);
return 0;
}
Explanation:
Keep track of the first and second largest as you iterate.
8. Write a program to implement a simple calculator using switch case.
cCopyEdit#include <stdio.h>
int main() {
float a, b;
char op;
printf("Enter expression (e.g. 5 + 3): ");
scanf("%f %c %f", &a, &op, &b);
switch (op) {
case '+': printf("Result = %.2f\n", a + b); break;
case '-': printf("Result = %.2f\n", a - b); break;
case '*': printf("Result = %.2f\n", a * b); break;
case '/':
if (b != 0)
printf("Result = %.2f\n", a / b);
else
printf("Division by zero error\n");
break;
default: printf("Invalid operator\n");
}
return 0;
}
Explanation:
Switch-case efficiently handles operator-based conditions.
9. Write a program to sort an array using bubble sort.
cCopyEdit#include <stdio.h>
int main() {
int arr[] = {5, 2, 9, 1, 5, 6}, n = 6;
for (int i = 0; i < n - 1; i++)
for (int j = 0; j < n - 1 - i; j++)
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
printf("Sorted array: ");
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Explanation:
Bubble sort compares each pair and swaps if in wrong order.
10. Write a program to read and write structure data to a file.
cCopyEdit#include <stdio.h>
struct Employee {
int id;
char name[50];
float salary;
};
int main() {
struct Employee e1 = {101, "Keshab", 45000.50};
FILE *fp = fopen("employee.dat", "wb");
fwrite(&e1, sizeof(e1), 1, fp);
fclose(fp);
struct Employee e2;
fp = fopen("employee.dat", "rb");
fread(&e2, sizeof(e2), 1, fp);
fclose(fp);
printf("ID: %d\nName: %s\nSalary: %.2f\n", e2.id, e2.name, e2.salary);
return 0;
}
Explanation:
We write the structure to a binary file and then read it back. This is useful in real-world applications.