Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,895 questions

51,826 answers

573 users

How to use a function with dynamic number of argument until zero with va_arg() in C

1 Answer

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

void PrintNumbers(int startnum, ...) {
    int count = 0;
    int n = startnum;
    
    va_list vl;
    va_start(vl, startnum);

    while (n != 0) {
        printf("%d\n", n);
        n = va_arg(vl, int);
    }

    va_end(vl);
}

int main(void)
{
    PrintNumbers(1, 2, 3, 4, 5, 0);

    return 0;
}



/*
run:

1
2
3
4
5

*/

 



answered Apr 21, 2016 by avibootz
edited Apr 26, 2024 by avibootz

Related questions

1 answer 137 views
1 answer 82 views
82 views asked Apr 23, 2022 by avibootz
2 answers 267 views
...