Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Hostgator 1 Penny

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


7,771 questions

10,854 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
...