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

51,811 answers

573 users

How to use terminator value to determine the end of va_list in C

1 Answer

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

double sum(double n, ...) 
{
  double sum = 0;
  va_list va;

  va_start(va, n);
  
  for (; !isnan(n); n = va_arg(va, double)) 
    sum += n;

  va_end(va);

  return sum;
}

int main (void) 
{
    printf("%g\n", sum(3.14, 2.3, 1.7, 6.8, 4.2, NAN));
    printf("%g\n", sum(0.1, 0.2, 0.5, 0.0313, 0.09, 0.7, NAN));
  
    return 0;
}

    
/*
run:
 
18.14
1.6213
   
*/

 



answered Aug 16, 2017 by avibootz
...