w3resource

C Programming Exercises, Practice, Solution : Variadic Function

C variadic function [8 Exercises with solution]

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

Variadic functions are functions in C that can accept a varying number of arguments. These functions are declared with an ellipsis (...) in their parameter list, which means that they can take any number of arguments of any type. Variadic functions are typically used in situations where the number of arguments or their types are not known at compile time, but rather determined at run time. Some common examples of variadic functions in C include printf() and scanf().

The <stdarg.h> header file in C provides a set of macros and functions that allow a variadic function to access the variable argument list. Some of the commonly used macros and functions provided by <stdarg.h> are:

  • va_list: A type to declare a variable that will hold the variable argument list.
  • va_start: A macro that initializes a va_list variable to point to the first argument in the variable argument list.
  • va_arg: A macro that retrieves the next argument in the variable argument list with a specified type.
  • va_copy: A macro that copies a va_list variable to another one.
  • va_end: A macro that cleans up the va_list variable after it has been used.

1. Write a C program to find the sum of a variable number of integers passed as arguments to a function using variadic functions.
Expected Output:

s1 = 6, s2 = 15, s3 = -6 

Click me to see the solution

2. Write a C program to find the product of a variable number of integers passed as arguments to a function using variadic functions.
Expected Output:

s1 = 6, s2 = 15, s3 = -6 

Click me to see the solution

3. Write a C program to find the maximum and minimum values of a variable number of integers passed as arguments to a function using variadic functions.
Expected Output:

Maximum value: 9
Minimum value: 0

Click me to see the solution

4. Write a C program to concatenate a variable number of strings passed as arguments to a function using variadic functions.
Expected Output:

Original strings:
String-1: w3resource
String-2: .
String-3: com
Concatenate said strings: w3resource.com

Click me to see the solution

5. Write a C program to count the number of characters in a variable number of strings passed as arguments to a function using variadic functions.
Expected Output:

Original strings:
String-1: w3resource
String-2: .
String-3: com
The total number of characters is 14

Click me to see the solution

6. Write a C program to find the average of a variable number of doubles passed as arguments to a function using variadic functions.
Expected Output:

The average is: 25.350000

Click me to see the solution

7. Write a C program to implement the printf() function using variadic functions.
Expected Output:

The values are: 10 20 30

Click me to see the solution

8. Write a C program to sort a variable number of integers passed as arguments to a function using variadic functions.
Expected Output:

1 3 5 7 9
-2 0 1

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.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/c-programming-exercises/variadic/index.php