Skip to main content

Variable arguments in C

In C language, its possible to write functions with variable arguments.  A function that takes variable number of arguments is also called as variadic function.  Below is an example prototype for a variadic function.

int func_name(int num, ...);
func_name     - name of the function
num               - number of arguments in argument list
...(ellipsis)     - used to denote variable arguments

Below are the list of macros availabe in 'stdarg.h' used to retrieve variable arguments.

void va_start (va_list arg, last_arg)
It initializes argument pointer arg to point to first optional argument and last_arg is the last named argument to a function.

type va_arg (va list arg, type) 
Returns subsequent optional argument and type is data type of the actual argument

void va_end (va list arg)
Ends the use of variable arguments.

Example C program on variable length argument list
 
#include <stdio.h>
#include <stdarg.h>
int sum(int, ...); // function prototype

int sum(int num, ...) {
int i, val, total = 0;
va_list arg;
/* initializes arg pointer to point to 1st optional arg */
va_start(arg, num); // num - no of arguments in parameter list
for (i = 0; i < 5; i++) {
val = va_arg(arg, int); // get subsequent argument
printf("Argument %d: %d\n", i + 1, val);
total = total + val;
}
va_end(arg); // end the use of variable argument
return (total);
}

int main() {
int res;
/* first argument to sum denotes the no of arguments in parameter list */
res = sum(5, 10, 20, 30, 40, 50);
printf("Resultant sum value is %d\n", res);
return 0;
}


  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Argument 1: 10
  Argument 2: 20
  Argument 3: 30
  Argument 4: 40
  Argument 5: 50
  Resultant sum value is 150



Comments

Popular posts from this blog

restorecrtmode example in c

Header file:     graphics.h Synopsis:        void restorecrtmode();       Description:       restorecrtmode() restores screen mode to text mode. restorecrtmode function in c graphics   #include <graphics.h>   #include <stdlib.h>   #include <stdio.h>   #include <conio.h>   int main(void) {         /* request auto detection */         int gd = DETECT, gmode, err;         int midx, midy;         /* initialize graphics and local variables */         initgraph(&gd, &gmode, "C:/TURBOC3/BGI");         /* read result of initialization */         err = graphresult();         if (err != grOk) {                 /* an error occurred */               ...

How to install JDK and setting environment variables for java

In this tutorial, we are going to see the following. How to install JDK How to run java program without setting java path How to set temporary java path in windows How to set permanent java path in windows How to install JDK? Check whether JDK is installed on your PC.  If it is not installed, please go to the below link and download JDK under Java SE(Java Standard Edition) in oracle website . Install the downloaded JDK in your PC. Once JDK is installed, you can run your java program using any method shown below. Having your java source code under jdk/bin, compile the source code and run the program from same directory Setting temporary path for java, compiling the source code and running the program from any location using command prompt Updating the java path in system or user variable(in system properties), compiling the source code and running the program anywhere using command prompt Let us see how to run java program without setting environmental variables.  Once java is ...

Save image in Microsoft Paint without white background

Here, we are going to see how to save image in mspaint without white background 1. Create your own drawing or painting 2. Crop the needed portion of your drawing or painting or image. Here, I am going to crop the porting inside the red box. Now, we have cropped the portion that needs to be saved as an image. 5. Clear the screen and paste the image which we cropped earlier. 6. Decrease the screen size by 50% 7. Now, adjust your screen to fit to your image size. 8. Save the image to desired file format(jpeg, png, bmp etc) 9. Go to the saved location and open the file.  Below is the output image of mine Hope you liked this article!!  For any suggestions, please comment below.