 # C Programming Code Examples

## C > Functions Code Examples

### Program to find maximum using var-args

``` Program to find maximum using var-args Write a C program to input two or more numbers from user and find maximum and minimum of the given numbers using functions. How to find maximum and minimum of two or more numbers using functions in C programming. We already learned to find maximum using conditional operator and using many other approaches. Here, I will embed the logic to find maximum within a function. Let us define function to find maximum. First give a meaningful name to our function. Say max() function is used to find maximum between two numbers. Second, we need to find maximum between two numbers. Hence, the function must accept two parameters of int type say, max(int number1, int number2). Finally, the function should return maximum among given two numbers. Hence, the return type of the function must be same as parameters type i.e. int in our case. After combining the above three points, function declaration to find maximum is int max(int number1, int number2);. #include <stdio.h> #include <limits.h> #include <stdarg.h> /* Function declarations */ int max(int args, ...); int min(int args, ...); int main() { /* Demonstrate the use of variable argument list */ printf("Maximum of three numbers: (12, 28, 36) = %d\n", max(4, 9, 30, 23)); printf("Maximum of five numbers: (5, -45, 4, 66, 88) = %d\n", max(5, -45, 4, 69, 100)); printf("Minimum of four numbers: (-5, 0, 16, 50) = %d\n", min(4, -5, 0, 10, 50)); printf("Minimum of two numbers: (10, 20) = %d", min(2, 10, 20)); return 0; } /** * Find maximum between two or more integer variables * @param args Total number of integers * @param ... List of integer variables to find maximum * @return Maximum among all integers passed */ int max(int args, ...) { int j, max, cur; va_list valist; va_start(valist, args); max = INT_MIN; for(j=0; j<args; j++) { cur = va_arg(valist, int); // Get next elements in the list if(max < cur) max = cur; } va_end(valist); // Clean memory assigned by valist return max; } /** * Find minimum between two or more integer variables * @param args Total number of integers * @param ... List of integer variables to find minimum * @return Minimum among all integers passed */ int min(int args, ...) { int j, min, cur; va_list valist; va_start(valist, args); min = INT_MAX; for(j=0; j<args; j++) { cur = va_arg(valist, int); if(min > cur) min = cur; } va_end(valist); return min; } ``` 