April 9th, 2007, 10:32 PM
Max function in C?
Is there a built-in function in C that would allow me to get the maximum between two values or a set of values?
maxVal = max(val1, val2)
April 9th, 2007, 10:54 PM
For float, double and long double there's the fmin(), fminf(), fminl(), fmax(), fmaxf(), fmasl() functions. I don't believe there is one that is portable for the various int values, but I'd bet most compilers have min() and max() macro's. If you have to do it in C, the easiest way is to write macro's like this:
Microsoft defines these in <minmax.h>.
#define max( a, b ) ( ((a) > (b)) ? (a) : (b) )
#define min( a, b ) ( ((a) < (b)) ? (a) : (b) )
I no longer wish to be associated with this site.
April 9th, 2007, 11:16 PM
Be aware of course, that macros aren't always a good solution. For instance, it is possible to mess up the above by doing this:
x = min(a++, b--);
As long as you don't put expressions like that in there, you should be ok though.
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
"Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
Down with Sharon Osbourne
"I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo
April 10th, 2007, 03:20 AM
I don't have those cases that you mentioned so I think I'm ok with this!