30 C Programming Basic Exercises for Advanced with Solutions

Master advanced C Programming skills with our comprehensive list of top 30 exercises. Dive into coding challenges that improve your understanding and proficiency in C Programming, setting a solid foundation for professional-level challenges. Start your journey to C Programming mastery today!

Learning Objectives:

By the end of these exercises, you will have mastered advanced C concepts, including file handling, complex data structures, multi-file projects, and performance optimization techniques.

Exercise Instructions:

  • Start with the first exercise and attempt to solve it before checking the hint or solution.
  • Ensure you understand the logic behind each solution, as this will help you in more complex problems.
  • Use these exercises to reinforce your learning and identify areas that may require further study.

1. Implement a program to create a dynamic array and calculate its average.

Required Input:

size = 5, elements = {10, 20, 30, 40, 50}

Expected Output:

Average = 30.00

Code In C

#include #include int main() { int size = 5; int *arr = (int *)malloc(size * sizeof(int)); // Add elements to arr double average; // Logic to calculate average free(arr); return 0; }

Output

Click Run Button to view compiled output

2. Create a program to store student records using a struct and display the details.

Required Input:

Student = {name = 'Alice', age = 21, grade = 'A'}

Expected Output:

Name: Alice, Age: 21, Grade: A

Code In C

#include struct Student { char name[50]; int age; char grade; }; int main() { struct Student student; // Add student details // Logic to display student details return 0; }

Output

Click Run Button to view compiled output

3. Implement a union to store either an integer or a float and display the value based on the type chosen.

Required Input:

choice = 'i', value = 10

Expected Output:

Integer Value: 10

Code In C

#include union Data { int intValue; float floatValue; }; int main() { union Data data; char choice; // Add logic to read choice and assign value // Logic to display based on choice return 0; }

Output

Click Run Button to view compiled output

4. Write a program to handle exceptions using setjmp and longjmp for error handling in C.

Required Input:

dividend = 10, divisor = 0

Expected Output:

Error: Division by zero

Code In C

#include #include jmp_buf buffer; void divide(int dividend, int divisor) { if (divisor == 0) { longjmp(buffer, 1); } else { printf("Result: %d\n", dividend / divisor); } } int main() { if (setjmp(buffer)) { printf("Error: Division by zero\n"); } else { divide(10, 0); } return 0; }

Output

Click Run Button to view compiled output

5. Create a linked list to store and display the elements.

Required Input:

elements = {5, 10, 15}

Expected Output:

5 -> 10 -> 15

Code In C

#include #include struct Node { int data; struct Node *next; }; int main() { struct Node *head = NULL; // Logic to add elements and display the list return 0; }

Output

Click Run Button to view compiled output

6. Write a program to perform matrix multiplication.

Required Input:

A = {{1, 2}, {3, 4}}, B = {{5, 6}, {7, 8}}

Expected Output:

Result Matrix:
19 22 
43 50 

Code In C

#include #define SIZE 2 int main() { int A[SIZE][SIZE] = {{1, 2}, {3, 4}}; int B[SIZE][SIZE] = {{5, 6}, {7, 8}}; int result[SIZE][SIZE] = {0}; // Logic for matrix multiplication return 0; }

Output

Click Run Button to view compiled output

7. Implement a program to find the maximum element in an array using pointers.

Required Input:

arr = {5, 3, 8, 1, 4}

Expected Output:

Maximum Element = 8

Code In C

#include int main() { int arr[] = {5, 3, 8, 1, 4}; int size = sizeof(arr) / sizeof(arr[0]); int *ptr = arr; // Logic to find maximum using pointers return 0; }

Output

Click Run Button to view compiled output

8. Write a program to implement a simple stack using an array and perform push and pop operations.

Required Input:

push(10), push(20), pop()

Expected Output:

Popped Element = 20

Code In C

#include #define MAX 10 struct Stack { int arr[MAX]; int top; }; int main() { struct Stack stack; stack.top = -1; // Logic to push and pop from the stack return 0; }

Output

Click Run Button to view compiled output

9. Implement a function to sort an array of integers using the Quick Sort algorithm.

Required Input:

arr = {5, 3, 8, 1, 4}

Expected Output:

Sorted Array: 1 3 4 5 8 

Code In C

#include void quickSort(int arr[], int low, int high); int main() { int arr[] = {5, 3, 8, 1, 4}; int size = sizeof(arr) / sizeof(arr[0]); // Logic to call quickSort return 0; }

Output

Click Run Button to view compiled output

10. Create a program to merge two sorted arrays into a single sorted array.

Required Input:

A = {1, 3, 5}, B = {2, 4, 6}

Expected Output:

Merged Array: 1 2 3 4 5 6 

Code In C

#include #define SIZE_A 3 #define SIZE_B 3 int main() { int A[SIZE_A] = {1, 3, 5}; int B[SIZE_B] = {2, 4, 6}; int merged[SIZE_A + SIZE_B]; // Logic to merge arrays return 0; }

Output

Click Run Button to view compiled output

ad vertical

1 of 3