C Programming: Variable Argument with deep explaination


Objective of this post: To explain clearly what Variable Argument in <stdarg.h> can do.


Syntax: 
            Please find its explanation at below link: 
            https://www.tutorialspoint.com/cprogramming/c_variable_arguments.htm
Sample 1: Sample from tutorialspoint.com
                     
#include <stdio.h>  
#include <stdarg.h>  

double average(int num,...) 
{ 
       /* initialize valist for number of arguments */
       va_list valist; 
       double sum = 0.0; 
       int i; 

       /* access all the arguments assigned to valist */ 
       va_start(valist, num);
       for (i = 0; i < num; i++) 
       { 
            sum += va_arg(valist, int);  
       }  

      /* clean reserved memory for valist */ 
      va_end(valist);  

      /* return average value */ 
      return sum/num;  
}  

int main()  
{ 
      printf("Average of 2, 3, 4, 5 = %f\n", average(4, 2,3,4,5)); 
      printf("Average of 5, 10, 15 = %f\n", average(3, 5,10,15));  
}
Online course sample



Sample 2: My experience that the online course doesn't explain to you clearly
#include <stdio.h>
#include <stdarg.h>  // header file for variable  
argument

#define FIND_MAX(a,b) (a > b ? a : b) // Define Macro to find max number of a and b then return max value

            double dbFindMaxGap(int number_of_argument_to_use ,...)
            {

        va_list valist;
        double ptrCurrentArg, dbTemp, dbMaxValue1, dbMaxValue2;
        int i;
                /* load argument to varlist just only number_of_argument_to_use arguments. it might NOT all of arguments */
        va_start(valist, number_of_argument_to_use);
        for(i=0 ; i<number_of_argument_to_use ; i++)
        {
             /* invoke argument as double datatype and you can invoke argument as any datatype.
                 if va_arg is called then ptrOfvarList will point to next argument automatically. */

             ptrCurrentArg = va_arg(valist, double);
             dbTemp = ptrCurrentArg;
             if(0==i) /* To protect human error like i=0 then use 0==i instead */
 
                 dbMaxValue1 = dbTemp;
             else
                 dbMaxValue1 = FIND_MAX(dbMaxValue1,dbTemp); 
                       /* To print only numbers we are focusing on for the 1st maximum */                        
                       printf("%ld, ",dbTemp);
       }
  
      va_start(valist, number_of_argument_to_use);
      for(i=0 ; i<number_of_argument_to_use ; i++)
      {
            ptrCurrentArg = va_arg(valist, double);
            dbTemp = ptrCurrentArg;
            if(dbMaxValue1!=dbTemp)  /* Skip if dbTemp is the same value of the 1st maximum */
            {
                if(0==i)
                    dbMaxValue2 = dbTemp;
                else
                    dbMaxValue2 = FIND_MAX(dbMaxValue2,dbTemp);   
           }
     } 
   va_end(valist); /* clean memory reserved for valist */
   printf("Maximum gap=%ld = %ld-%ld\n\n",(dbMaxValue1-dbMaxValue2),dbMaxValue1,dbMaxValue2);
   return (dbMaxValue1-dbMaxValue2);    /* return MaxGap of numbers */ 
}
int main(int argc, char **argv)
{
   dbFindMaxGap(4,20,3,400,5);   // 4 tells dbFindMaxGap to use only 4 params of 4 = 20,3,400,5
   dbFindMaxGap(3,5,10,15,5,8);  // 3 tells dbFindMaxGap to use only 3 params of 5 = 5,10,15
   dbFindMaxGap(3,10,10,10);      // 3 tells dbFindMaxGap to use only 3 params of 3 = 10,10,10
  
   printf("\n\nThis is a sample, you may simplify function, modify or any your purpose\n");
   return 0; /* This return the online course forgot since main have to return int */
}













You may leave comment to improve it. 
Thank you.

Popular posts from this blog

C Programming: How to improve C to support more Array N dimensions

C Programming: Superb E-Book from beginner to expert [C career semi retired]

C Programming: Understand For loop syntax in deep detail