Where are MIN and MAX defined in C, if at all?

They aren't.

What is the best way to implement these, as generically and type safe as possible (compiler extensions/builtins for mainstream compilers preferred).

As functions. I wouldn't use macros like #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)), especially if you plan to deploy your code. Either write your own, use something like standard fmax or fmin, or fix the macro using GCC's typeof (you get typesafety bonus too) in a GCC statement expression:

 #define max(a,b) \
   ({ __typeof__ (a) _a = (a); \
       __typeof__ (b) _b = (b); \
     _a > _b ? _a : _b; })

Everyone says "oh I know about double evaluation, it's no problem" and a few months down the road, you'll be debugging the silliest problems for hours on end.

Note the use of __typeof__ instead of typeof:

If you are writing a header file that must work when included in ISO C programs, write __typeof__ instead of typeof.

Answer from David Titarenco on Stack Overflow
Top answer
1 of 16
546

Where are MIN and MAX defined in C, if at all?

They aren't.

What is the best way to implement these, as generically and type safe as possible (compiler extensions/builtins for mainstream compilers preferred).

As functions. I wouldn't use macros like #define MIN(X, Y) (((X) < (Y)) ? (X) : (Y)), especially if you plan to deploy your code. Either write your own, use something like standard fmax or fmin, or fix the macro using GCC's typeof (you get typesafety bonus too) in a GCC statement expression:

 #define max(a,b) \
   ({ __typeof__ (a) _a = (a); \
       __typeof__ (b) _b = (b); \
     _a > _b ? _a : _b; })

Everyone says "oh I know about double evaluation, it's no problem" and a few months down the road, you'll be debugging the silliest problems for hours on end.

Note the use of __typeof__ instead of typeof:

If you are writing a header file that must work when included in ISO C programs, write __typeof__ instead of typeof.

2 of 16
129

It's also provided in the GNU libc (Linux) and FreeBSD versions of sys/param.h, and has the definition provided by dreamlax.


On Debian:

$ uname -sr
Linux 2.6.11

$ cat /etc/debian_version
5.0.2

$ egrep 'MIN\(|MAX\(' /usr/include/sys/param.h
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))

$ head -n 2 /usr/include/sys/param.h | grep GNU
This file is part of the GNU C Library.

On FreeBSD:

$ uname -sr
FreeBSD 5.5-STABLE

$ egrep 'MIN\(|MAX\(' /usr/include/sys/param.h
#define MIN(a,b) (((a)<(b))?(a):(b))
#define MAX(a,b) (((a)>(b))?(a):(b))

The source repositories are here:

  • GNU C Library
  • FreeBSD
🌐
Linux Hint
linuxhint.com › min-function-c
MIN() Macro in C Language – Linux Hint
To do this, we include the “stdio.h” and “param.h” headers and open a main() function of type void. We define the “a” and “b” integers in it and assign them with a random value. We also define the “c” integer to store the result. Then, we call the macro MIN() and pass “a” and “b” as input arguments and “c” as the output argument.
Discussions

min/max in *c*
But what is the proper header to include for min & max? Your framerate proves your lack of manhood ... 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... More on gamedev.net
🌐 gamedev.net
8
July 29, 2008
c++ min and max from algorithm work without including this header, how?
Blind shot into the dark here as I'm not very familiar with C++ libraries, but it seems that according to: https://code.woboq.org/gcc/libstdc++-v3/include/std/functional.html functional has an include for which seems to be the header file for algorithm. So, I'm guessing that by including functional you're also including the files it includes. One of those includes happens to be algorithm so you're allow to use it. link to the algorithm header file: https://code.woboq.org/gcc/libstdc++-v3/include/bits/stl_algo.h.html More on reddit.com
🌐 r/learnprogramming
6
3
December 3, 2021
🌐
Embarcadero
docwiki.embarcadero.com › RADStudio › Sydney › en › Min
min (C++) - RAD Studio
Header File · stdlib.h · Category · C++ Prototyped Routines · Prototype · (type) min(a, b); /* macro version */ template <class T> T min( T t1, T t2 );// C++ only · Description · Returns the smaller of two values. The C macro and the C++ template function compare two values and return ...
🌐
Ticalc
tigcc.ticalc.org › doc › stdlib.html
stdlib.h
However, strcmp, which is frequently used as a comparison function, returns a short integer. Here is a complete example of usage (called "Sort Integers"): // Sort a list of integer values #define USE_TI89 // Compile for TI-89 #define USE_TI92PLUS // Compile for TI-92 Plus #define USE_V200 // Compile for V200 #define MIN_AMS 100 // Compile for AMS 1.00 or higher #define SAVE_SCREEN // Save/Restore LCD Contents #include <tigcclib.h> // Include All Header Files // Comparison Function CALLBACK short int_comp(const void *a, const void *b) { return fcmp (*(const short*)a, *(const short*)b); } // Main Function void _main(void) { short list[10] = {2, 9, 3, 6, 4, 2, 3, 3, 1, 5}; int i; clrscr (); qsort (list, 10, sizeof (short), int_comp); for (i = 0; i < 10; i++) printf ("%d ", list[i]); ngetchx (); } Note that the function strcmp is ideal for string comparisons.
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › min
__min | Microsoft Learn
October 26, 2022 - // crt_minmax.c #include <stdlib.h> #include <stdio.h> int main( void ) { int a = 10; int b = 21; printf( "The larger of %d and %d is %d\n", a, b, __max( a, b ) ); printf( "The smaller of %d and %d is %d\n", a, b, __min( a, b ) ); }
🌐
Cplusplus
cplusplus.com › reference › algorithm › min
std::min
Returns the smallest of a and b. If both are equivalent, a is returned. The versions for initializer lists (3) return the smallest of all the elements in the list. Returning the first of them if these are more than one. The function uses operator< (or comp, if provided) to compare the values.
Find elsewhere
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-3.3 › gcc › Min-and-Max.html
Using the GNU Compiler Collection (GCC)
For example, MIN (i++, j++) will fail, incrementing the smaller counter twice. The GNU C typeof extension allows you to write safe macros that avoid this kind of problem (see Typeof). However, writing MIN and MAX as macros also forces you to use function-call notation for a fundamental arithmetic ...
🌐
Delft Stack
delftstack.com › home › howto › c max and min function
MIN and MAX Function in C | Delft Stack
October 12, 2023 - Inside the MAX() and MIN() function, we stored the first element of the array in a variable, and then we compared it with all the other elements of the array using a loop that will stop when the integer i becomes equal to the length of the array which means the loop has reached the end of the array.
🌐
Cppreference
en.cppreference.com › w › cpp › algorithm › min.html
std::min - cppreference.com
December 5, 2024 - int n = -1; const int& r = std::min(n + 2, n * 2); // r is dangling ... #include <algorithm> #include <iostream> #include <string_view> int main() { std::cout << "smaller of 10 and 010 is " << std::min(10, 010) << '\n' << "smaller of 'd' and 'b' is '" << std::min('d', 'b') << "'\n" << "shortest of \"foo\", \"bar\", and \"hello\" is \"" << std::min({"foo", "bar", "hello"}, [](const std::string_view s1, const std::string_view s2) { return s1.size() < s2.size(); }) << "\"\n"; }
🌐
GameDev.net
gamedev.net › forums › topic › 503081-minmax-in-c › 4282091
min/max in *c* - For Beginners - GameDev.net
July 29, 2008 - But what is the proper header to include for min & max? <SkilletAudio> Your framerate proves your lack of manhood ... 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.
🌐
W3Schools
w3schools.com › c › ref_math_fmin.php
C Math fmin() Function
C Examples C Real-Life Examples ... Try it Yourself » · The fmin() function returns the number with the lowest value from a pair of numbers. The fmin() function is defined in the <math.h> header file....
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-3.4.6 › gcc › Min-and-Max.html
Min and Max - Using the GNU Compiler Collection (GCC)
For example, MIN (i++, j++) will fail, incrementing the smaller counter twice. The GNU C typeof extension allows you to write safe macros that avoid this kind of problem (see Typeof). However, writing MIN and MAX as macros also forces you to use function-call notation for a fundamental arithmetic ...
🌐
Edureka Community
edureka.co › home › community › categories › c++ › use of min and max functions in c
Use of min and max functions in C | Edureka Community
June 6, 2022 - Are std::min and std::max better than fmin and fmax in C++? Do they provide essentially the ... C standard (C99). Thank you very much in advance!
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › stdmin-in-cpp
std::min in C++ - GeeksforGeeks
May 19, 2025 - The std::min() is used to find the minimum element among the given elements. It is the built-in function of C++ STL defined inside <algorithm> header file.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › int_max-int_min-cc-applications
INT_MAX and INT_MIN in C/C++ and Applications - GeeksforGeeks
May 13, 2025 - INT_MAX and INT_MIN are predefined macros provided in C/C++ to represent the integer limits. Depending upon the compiler and C++ standard, you may be required to include the header file <limits.h> or <climits> in your C or C++ source code, respectively.
🌐
Launchpad
answers.launchpad.net › ubuntu › +source › gnome-terminal › +question › 82506
Question #82506 “max() or min() functions in stdlib.h” : Questions : gnome-terminal package : Ubuntu
As far as I'm aware, there is no max or min function in the C or GNU standard libraries. There is fmax/fmin, but since max and min are so trivial to write ...
🌐
Reddit
reddit.com › r/c_language › add integer functions min and max to c language?
r/c_language on Reddit: Add integer functions min and max to C language?
July 30, 2023 -

The C programming language is missing library functions min and max for integer types. I am not aware of any argument to not provide them, given virtually every programming language providing them.

Relevance

For floats and doubles, fmin and fmax exists, but not for integer types. Other programming language provide them including C++ by std::min and std::max. Several C libraries provide them too, e.g., Linux, SuperLU, Cairo. Stackoverflow has a question with more then 400 upvotes, indicating that this is a real issue.

Add to C language?

Is it worthwhile to write a paper suggesting the addition of min and max? Or is there a reason that their addition has not already happened?

🌐
Cprogramming
cboard.cprogramming.com › c-programming › 177146-min-max-mean.html
Min, Max, Mean
February 27, 2019 - I'm trying to write a C code in codeblocks to ask a user for the amount of numbers they want to enter, then output the min, max, and average. the aver
🌐
Cplusplus
cplusplus.com › reference › algorithm › max
Cplusplus
Values to compare. ... Binary function that accepts two values of type T as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered less than the second. The function shall not modify any of its arguments.