min/max in *c*

Started by
8 comments, last by AcidZombie24 15 years, 8 months ago
What include has min and max? I need it for a C file. Right now i have defines. But what is the proper header to include for min & max?
<SkilletAudio> Your framerate proves your lack of manhood
Advertisement
#define min(a, b) (((a) < (b)) ? (a) : (b))
#define max(a, b) (((a) > (b)) ? (a) : (b))

Macros are dangerous in this situation because a or b will get evaluated twice, which would cause unexpected behavior on expressions whose evaluation has side effects (like a function) Beware.
I don't believe those functions are defined in the C standard lib. In C++ they are a part of the STL. Seriously you got to roll out the big guns to write min and max:)
Quote:Original post by AcidZombie24
What include has min and max?
I need it for a C file. Right now i have defines. But what is the proper header to include for min & max?


Did you know that you can search the include folder to find out? If you're not sure which headers are part of the standard, you can search the internet using google to find out which headers are standard and which are not.

The header file that you want is stdlib.h
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
I believe these were added in C99, but they are in math.h:

double fmax(double x, double y);
float fmaxf(float x, float y);
long double fmaxl(long double x, long double y);
double fmin(double x, double y);
float fminf(float x, float y);
long double fminl(long double x, long double y);

They are in 7.12.12 of the C99 draft. I tried searching math.h with Visual Studio 2005, and came up empty. If I were a betting man, I'd say these are C99 additions.
-- gekko
I think <windows.h> defines them if theyre not defined.
Quote:Original post by thre3dee
I think <windows.h> defines them if theyre not defined.


Windows does define them, but as a C++ programmer I always #define NOMINMAX to stop the macros from treading on the STL std::min and std::max. If you're going to use macros, please use upper case naming like MIN(x,y) and MAX(x,y) so that it's obvious what they are.
If you can't FIND the right function, why not just write your own? It's got to be the easiest generic function I know...although might be a little harder than a function which converts a boolean to a string, "true" or "false".
Quote:Original post by LessBread
Quote:Original post by AcidZombie24
What include has min and max?
I need it for a C file. Right now i have defines. But what is the proper header to include for min & max?


Did you know that you can search the include folder to find out? If you're not sure which headers are part of the standard, you can search the internet using google to find out which headers are standard and which are not.

The header file that you want is stdlib.h


I search msvc and found it in minmax.h, i search that and found it included in no headers. So i thought it might be hidden in a precompiled header or something strange like that.

I googled both stdlib and math and both did not come up with min/max

tommorow night i'll try including those headers and see if it works on all platforms.

Thanks for the reply guys

stonemetal: i think your right, i dont think it is in a standard header.
<SkilletAudio> Your framerate proves your lack of manhood

This topic is closed to new replies.

Advertisement