 # 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; } ```

C++ Increment and Decrement Operators - In this C++ example, you'll 'learn to overload' Increment ++ and Decrement -- operators in C++. "increment & decrements" operator are overloaded in best possible way, increase the

Find Sum of First & Last digit of a number - C program to input a number and find sum of first & last digit of the number using for loop. Input a number from user and store it in some variable say j. Find last digit of given number

C++ Multiply Two Matrices use 2D Arrays - C++ Code is very simple it takes input in two 2D arrays each with size of 2X2. After taking the input program "multiply two matrix" in 3 nested for loops. Size of arrays is defined use

C If Expression is Correctly Parenthesized - C program code takes a expression as input and checks if the expression is correctly parenthesized. Take a expression as input and store it in the array. Check for the "(" ...

Checks for Balanced Paranthesis by Stacks - Program sample, using a stack data strucure, computing whether the given "Parantheses" expression is 'valid' or not by check whether each parentheses is closed and nested in the