w3resource

C Programming Exercises, Practice, Solution : Callback Function

C Callback function [11 Exercises with solution]

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

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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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.