Skip to content

Instantly share code, notes, and snippets.

@ohmree
Last active September 25, 2016 16:32
Show Gist options
  • Save ohmree/f0db39fd6f97631a926311493131d0fc to your computer and use it in GitHub Desktop.
Save ohmree/f0db39fd6f97631a926311493131d0fc to your computer and use it in GitHub Desktop.
Min and max functions in c
#include "stdio.h"
#include "stdlib.h"
#include "stdarg.h"
/**
* The maxOf function takes in any number of signed integers preceded by the amount of arguments (not including the first one) that
* it receives and returns the biggest number amongst all of it's arguments
* example usage:
* maxOf(5, 56, 34, 345, 1235, 90686); should return 90686*/
int maxOf(int num, ...)
{
va_list args;
int max;
/*register keyword may not be needed*/
register int i;
va_start(args, num);
for (i = 0; i < num; i++)
{
int a = va_arg(args, int);
if (a > max) max = a;
}
va_end(args);
return max;
}
/**
* The minOf function takes in any number of signed integers preceded by the amount of arguments (not including the first one) that
* it receives and returns the smallest number amongst all of it's arguments
* example usage:
* minOf(5, 56, 34, 345, 1235, 90686); should return 5*/
int minOf(int num, ...)
{
va_list args;
int min;
/*register keyword may not be needed*/
register int i;
va_start(args, num);
for (i = 0; i < num; i++)
{
int a = va_arg(args, int);
if (a < min) min = a;
}
va_end(args);
return min;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment