How to write a dynamic number of strings to a function in C

2 Answers

0 votes
#include <stdio.h> 
#include <stdarg.h>

void function(char* format, ...) {
    va_list vl;
    va_start(vl, format);
    
    vprintf(format, vl);

    va_end(vl);
}

int main()
{
    function("%s %s %s %s", "c", "c++", "java", "python");

    return 0;
}



/*

run:

c c++ java python

*/

 



answered Apr 26, 2024 by avibootz
0 votes
#include <stdio.h> 
#include <stdarg.h>

void function(char* first, ...) {
    va_list vl;

    va_start(vl, first);
    char* s = first;

    while (s != 0) {
        puts(s);
        s = va_arg(vl, char*);
    }
    
    va_end(vl);
}

int main()
{
    function("c", "c++", "java", "python", "rust");

    return 0;
}



/*

run:

c
c++
java
python
rust

*/

 



answered Apr 26, 2024 by avibootz

Related questions

2 answers 230 views
2 answers 261 views
2 answers 290 views
1 answer 265 views
...