in Programming retagged by
988 views
2 votes
2 votes

Output of following program? 

#include<stdio.h>
void dynamic(int s,...) 
{ 
printf("%d",s); 
} 
int main() 
{ 
dynamic(2,4,6,8); 
dynamic(3,6,9); 
return 0; 
}
  1. $2\:3$
  2. Compiler Error
  3. $4\:3$
  4. $3\:2$
in Programming retagged by
988 views

1 Answer

4 votes
4 votes

dynamic is a function with variable number of arguments.

Here it prints only s.

So A is correct.

See below example to know how multiple arguments passed can be used.

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

double average(int num,...) {

   va_list valist;
   double sum = 0.0;
   int i;

   /* initialize valist for num number of arguments */
   va_start(valist, num);

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

   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));
}

 

Average of 2, 3, 4, 5 = 3.500000
Average of 5, 10, 15 = 10.000000

Ref: https://www.tutorialspoint.com/cprogramming/c_variable_arguments.htm

Answer:

Related questions