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
🌐
Codecademy
codecademy.com › docs › c# › math functions › .min()
C# (C Sharp) | Math Functions | .Min() | Codecademy
March 2, 2023 - Math.Min() returns the lesser value from num1 and num2. The following example compares two integers i1 and i2 and writes the lesser integer to the console.
🌐
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!
Discussions

MIN and MAX in C - Stack Overflow
As a result, I've been getting better at using templates, and want to put in the C++ template versions here for completeness and to make this a more canonical and thorough answer. Here's what basic function template versions of max() and min() might look like in C++: More on stackoverflow.com
🌐 stackoverflow.com
Add integer functions min and max to C language?
C is not a batteries included language. These are extremely easy to implement using a function or macro. Literally one-liners that are impossible to screw up. The reason why fmin et al exist is that there are eccentricities to floating point types and it's not a simple comparison. For example, is 7 > NAN? More on reddit.com
🌐 r/c_language
4
2
July 30, 2023
min max and sign functions in CUDA do they exist? if so where?
Hello, I was looking for min, max and sign functions in CUDA, but I found nothing. I am using the very basic macro definitions for them and I don’t like it much, because they could lead to warp divergence Now with Fermi there is something called predication, which I don’t understand well, ... More on forums.developer.nvidia.com
🌐 forums.developer.nvidia.com
0
0
February 25, 2012
Reddit - The heart of the internet
Reddit is where millions of people gather for conversations about the things they care about, in over 100,000 subreddit communities. More on reddit.com
🌐 reddit.com
3 days ago
🌐
C For Dummies
c-for-dummies.com › blog
Min and Max | C For Dummies Blog
September 1, 2013 - array_max() This function returns the highest (maximum) value of all the values stored in the array. array_min() This function returns the lowest (minimum) value stored in the array.
🌐
Quora
quora.com › In-C-the-max-and-min-function-only-allow-two-numbers-so-how-do-you-find-the-max-of-four
In C++, the max() and min() function only allow two numbers, so how do you find the max of four? - Quora
Answer (1 of 5): Why not write your own recursive max function? The function could take an arbitrarily long list of integers as its input. If the length of the list is two, then it would return the C++ [code ]max[/code] of the two elements in the list. Otherwise, return the C++ [code ]max[/code] ...
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
🌐
Embarcadero
docwiki.embarcadero.com › RADStudio › Athens › en › Min
min (C++) - RAD Studio
Go Up to stdlib.h Index · 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 ...
Find elsewhere
🌐
Particle
docs.particle.io › reference › device-os › api › math › min
min() - Math | Reference | Particle
min(a++, 100); // avoid this - yields incorrect results a++; min(a, 100); // use this instead - keep other math outside the function
🌐
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?

🌐
Linux Hint
linuxhint.com › min-function-c
MIN() Macro in C Language – Linux Hint
Practical guide on how to use the macro MIN() to find the minimum value of two variables, how it works, and the expression and formula that this macro applies.
🌐
Qnx
qnx.com › developers › docs › 6.3.2 › neutrino › lib_ref › m › min.html
min()
The min() function returns the lesser of two values. #include <stdio.h> #include <stdlib.h> int main( void ) { int a; a = min( 1, 10 ); printf( "The value is: %d\n", a ); return EXIT_SUCCESS; }
🌐
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.
🌐
NVIDIA Developer Forums
forums.developer.nvidia.com › accelerated computing › cuda › cuda programming and performance
min max and sign functions in CUDA do they exist? if so where? - CUDA Programming and Performance - NVIDIA Developer Forums
February 25, 2012 - Hello, I was looking for min, max and sign functions in CUDA, but I found nothing. I am using the very basic macro definitions for them and I don’t like it much, because they could lead to warp divergence Now with Fermi there is something called predication, which I don’t understand well, ...
🌐
Educative
educative.io › answers › how-to-use-min-function-in-cpp
How to use min() function in C++
The min() function in C++ accepts two values and returns the smaller one.
🌐
Blogger
betterembsw.blogspot.com › 2017 › 07 › dont-use-macros-for-min-and-max.html
Better Embedded System SW: Don't use macros for MIN and MAX
There are fancy hacks to try to get any particular macros such as MIN and MAX to be better behaved, but no matter how hard you try you're really just making a deal with the devil. ... The fix is: don't use macros. Instead use inline procedure calls. You should already have access to built-in functions for floating point such as fmin() and fmax().
🌐
Ticalc
tigcc.ticalc.org › doc › stdlib.html
stdlib.h
min is an inline function (implemented using GNU C smart macros) which returns the smaller of a and b. They may be any numeric values, either integer or floating point numbers, and they also may be pointers to the same base type. The result has the type of the argument which has greater range ...
🌐
W3Schools
w3schools.com › c › ref_math_fmin.php
C Math fmin() Function
Tip: Use the fmax() function to return the number with the highest value. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 337349 › min-and-max-of-a-function-in-c
Min and max of a function in C++ | DaniWeb
Even some very simple functions have large number of mimina/maxima in an interval and determining the global maxima is difficult. Given that, here are two approaches that you might like to consider, first is to calculate the partial derivatives and solve the simulation equation you get when you set both to zero. Note that you will, get maximums, minimiums and saddle points [dependent on the function]. The find the range that you have selected and if any are valid.
🌐
Linux Hint
linuxhint.com › min-and-max-in-c
Min and Max in C – Linux Hint
The user-defined function may be ... the built-in functions in C called ‘fmin()’ and ‘fmax()’. These functions fetch the min or max element from a specific subset or range 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
🌐
w3resource
w3resource.com › c-programming-exercises › inline_function › c-inline-function-exercise-3.php
C inline function - Compute the minimum of two integers
November 1, 2025 - In the above program, we define an inline function min() that takes two integers x and y as parameters and returns the minimum value between them. We use the ternary operator ? : to check which value is smaller and return it.