Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

How to use variadic function to get max of a set of numbers in C

1 Answer

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

// int function_name(data_type variable_name, ...);

int max(int total, ...) {
	va_list ptr;

	va_start(ptr, total);

	int max = va_arg(ptr, int);

	for (int i = 0; i < total - 1; i++) {
		int temp = va_arg(ptr, int);
		max = temp > max ? temp : max;
	}

	va_end(ptr);

	return max;
}

int main() {
	printf("%d\n", max(6, 4, 8, 2, 7, 9, 0));

	printf("%d\n", max(3, 45, 100, 99));

	return 0;
}



/*
run:
 
9
100
 
*/

 



answered Dec 7, 2022 by avibootz

Related questions

1 answer 102 views
1 answer 103 views
1 answer 106 views
1 answer 111 views
111 views asked Dec 6, 2022 by avibootz
1 answer 137 views
...