w3resource

C Programming Exercises, Practice, Solution : Callback Function


This resource offers a total of 55 C Callback function problems for practice. It includes 11 main exercises, each accompanied by solutions, detailed explanations, and four related problems

[An Editor is available at the bottom of the page to write and execute the scripts.]

In computer programming, a callback or callback function is any reference to executable code that is passed as an argument to another piece of code; that code is expected to call back (execute) the callback function as part of its job. Callback functions can be used to implement event handlers, or to allow a function to be customized by the caller.


1. Array Square Callback Variants

Write a C program to print the square of array elements using callback function.

Expected Output:

Array elements before processing: 1 2 3 4 5 6 
Square of the array elements after processing: 1 4 9 16 25 36 

Click me to see the solution


2. Array Sorting with Callback Variants

Write C program to sort an array of integers in ascending or descending order using a callback function to compare the elements.

Expected Output:

Original array: 7 2 0 5 8 9 

Ascending order: 0 2 5 7 8 9 

Descending order: 9 8 7 5 2 0

Click me to see the solution


3. Sum/Product Callback Variants

Write a C program to calculate the sum or product of an array of integers using a callback function.

Expected Output:

Original array elements: 10 20 30 40 50 60 
Sum: 210
Product: 720000000

Click me to see the solution


4. Palindrome String Callback Variants

Write a C program to check if a string (case-sensitive) is a palindrome or not using a callback function.

Expected Output:

String: Madam
Madam is not a palindrome (case-sensitive).
Madam is a palindrome (case-insensitive).

String: aba
aba is a palindrome (case-sensitive).
aba is a palindrome (case-insensitive).

Click me to see the solution


5. String Case Conversion Callback Variants

Write a program in C program to convert a string to uppercase or lowercase using a callback function to modify each character.

Expected Output:

Input a string: w3resource
Select an option:
1. Convert to uppercase
2. Convert to lowercase
1
Uppercase string: W3RESOURCE

Click me to see the solution


6. Average/Median Calculation Callback Variants

Write a program in C program to calculate the average or median of an array of integers using a callback function to perform the calculation.

Expected Output:

Original array elements: 2 5 4 7 1 8 4 6 5 9 10 

Select an option:
1. Calculate average of the said array elements:
2. Calculate median of the said array elements:
Average: 5.545455

Click me to see the solution


7. Whitespace Removal Callback Variants

Write a C program to remove all whitespace from a string using a callback function.

Expected Output:

Enter a string: Original string: example     .   com
String without whitespace: example.com

Click me to see the solution


8. Min/Max Element Callback Variants

Write a C program to find the largest or smallest element in an array using a callback function to compare the elements.

Expected Output:

Original array elements: 7 0 4 2 9 5 1 

The minimum element is 0
The maximum element is 9

Click me to see the solution


9. Array Shuffle Callback Variants

Write a C program to shuffle the elements of an array using a callback function to generate random numbers for swapping.

Expected Output:

Original array elements: 1 2 3 4 5 
Shuffled array elements: 3 5 2 1 4 

Click me to see the solution


10. Quick Sort Callback Variants

Write a C program to implement quick sort using callback function.

Expected Output:

Before sorting: 5 2 8 7 1 3 6 4 
After sorting: 1 2 3 4 5 6 7 8 

Click me to see the solution


11. Binary Search Callback Variants

Write a program in C program to used to perform a binary search for an element in a sorted array using callback function.

Expected Output:

Before sorting: 5 2 8 7 1 3 6 4 
After sorting: 1 2 3 4 5 6 7 8 

Click me to see the solution


C Programming Code Editor:



More to Come !

Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.



Follow us on Facebook and Twitter for latest update.