In programming, it is common to print strings in various scenarios. Having a custom function that handles printing multiple strings with additional options can greatly simplify code and enhance readability. In this post, we will tackle the task of creating a function that prints strings, followed by a new line with variadic functions in C. We will explore the required prototype, discuss the logic behind the implementation, and provide a code solution that meets the given specifications.
Task Description:
Our task is to write a function called “print_strings” that takes in a separator string, the number of strings to print, and a variable number of string arguments. The function will print the strings, separated by the provided separator, and end with a new line. If the separator is NULL, it should be omitted. Additionally, if any of the string arguments are NULL, they should be printed as “nil”.
Prototype:
void print_strings(const char *separator, const unsigned int n, ...);
Code Solution:
Let’s delve into the code solution for this task:
#include <stdio.h>
#include <stdarg.h>
void print_strings(const char *separator, const unsigned int n, ...) {
unsigned int i;
char *str;
va_list ap;
va_start(ap, n);
for (i = 0; i < n; i++) {
str = va_arg(ap, char *);
if (str == NULL) {
printf("nil");
} else {
printf("%s", str);
}
if (i < n - 1 && separator != NULL) {
printf("%s", separator);
}
}
va_end(ap);
printf("\n");
}
Explanation:
Let’s break down the code and explain each part:
- We include the necessary headers: <stdio.h> for input/output operations and <stdarg.h> for handling variadic arguments.
- The function “print_strings” takes in the separator string as a
const char*
, the number of stringsn
as anunsigned int
, and uses variadic arguments (...
) to accept a variable number of string arguments. - Inside the function, we declare variables:
i
for the loop counter,str
to hold each string argument, andap
as the “va_list” variable to access the variadic arguments. - We initialize the
ap
variable using “va_start” macro, passing inap
andn
. - We iterate
n
times to process each string argument. - Inside the loop, we retrieve the next string argument using “va_arg” and assign it to the
str
variable. - We check if the
str
isNULL
. If it is, we print “(nil)”. Otherwise, we print the string usingprintf("%s", str)
. - Next, we check if the separator is not
NULL
and if we are not on the last string. If both conditions are met, we print the separator usingprintf("%s", separator)
. - After the loop, we print a new line using
printf("\n")
. - Finally, we clean up the variadic arguments using
va_end(ap)
.
Output and Usage:
To test the function, you can call it with different arguments, as shown below:
int main(void)
{
print_strings(", ", 2, "Jay", "Django");
printf("-------------------------\n");
print_strings(", ", 4, "Hello", "world", NULL, "Rocodeify");
printf("-------------------------\n");
print_strings(" - ", 3, "One", "Two", "Three");
printf("-------------------------\n");
print_strings(NULL, 3, "Apple", "Banana", "Cherry");
return (0);
}
Expected Output:

Conclusion:
Congratulations! You have successfully implemented a function that prints strings, separated by a given separator and followed by a new line. Feel free to try out different scenarios and string combinations to ensure the function meets your requirements.
Now it’s your turn!
Give the print_strings function a try and leave a comment below sharing your thoughts or any questions you may have.
If you have any challenging programming tasks or need further assistance, don’t hesitate to join our forum at Rocodeify Forum and engage with our growing community. We’re here to support you in your programming journey.
Happy coding!