Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- function_with_variable_list_of_arguments.c
- https://www.tutorialspoint.com/cprogramming/c_variable_arguments.htm
- You can find all my C programs at Dragan Milicev's pastebin:
- https://pastebin.com/u/dmilicev
- */
- #include <stdio.h>
- #include <stdarg.h> // for variable list of arguments, va_start() and va_end()
- double average(char text[],int num,...) {
- va_list valist;
- double sum = 0.0;
- int i;
- va_start(valist, num); // initialize valist for num number of arguments
- for (i = 0; i < num; i++) // access all the arguments assigned to valist
- sum += va_arg(valist, int);
- va_end(valist); // clean memory reserved for valist
- printf("\n %s \n", text);
- return sum/num;
- }
- int meny( char meny_title[],
- int number_of_meny_items, ... )
- {
- int i;
- va_list valist;
- printf("\n%s\n", meny_title);
- va_start(valist, number_of_meny_items); // initialize valist for num number of arguments
- for (i = 0; i < number_of_meny_items; i++) // access all the arguments assigned to valist
- printf("%s\n", va_arg(valist, char*) );
- va_end(valist); // clean memory reserved for valist
- return 0;
- }
- int main(void)
- {
- printf("Average of 2, 3, 4, 5 = %f \n", average("This is text",4, 2,3,4,5) );
- printf("Average of 5, 10, 15 = %f \n", average("This is text",3, 5,10,15) );
- meny( "MENY TITLE",
- 5,
- "1. first item",
- "2. second item",
- "3. third item",
- "4. fourth item",
- "5. fifth item" );
- return 0;
- } // main()
Advertisement
Add Comment
Please, Sign In to add comment