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,971 questions

51,913 answers

573 users

How to get the length of va_list when using variable list arguments in C

2 Answers

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

void printNumbers(int sentinel, ...) {
    va_list args;
    va_start(args, sentinel);

    int count = 0;
    int num;
    while ((num = va_arg(args, int)) != sentinel) {
        printf("%d ", num);
        count++;
    }

    va_end(args);
    printf("\nNumber of arguments: %d\n", count);
}

int main() {
    printNumbers(-1, 5, 8, 9, 0, -1); 
    
    return 0;
}



/*
run:

5 8 9 0 
Number of arguments: 4

*/

 



answered Mar 1, 2025 by avibootz
edited Mar 1, 2025 by avibootz
0 votes
#include <stdio.h>
#include <stdarg.h>

void printNumbers(int numArgs, ...) {
    va_list args;
    va_start(args, numArgs);

    for (int i = 0; i < numArgs; i++) {
        int num = va_arg(args, int);
        printf("%d ", num);
    }

    va_end(args);
    printf("\nNumber of arguments: %d\n", numArgs);
}

int main() {
    printNumbers(5, 1, 7, 0, 9, 8);
}



/*
run:

1 7 0 9 8 
Number of arguments: 5

*/

 



answered Mar 1, 2025 by avibootz
...