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-and-max-in-c
Min and Max in C – Linux Hint
However, if a user doesn’t want to go into a detailed process, they can go with using 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. To use the ‘fmin’ and ‘fmax’ functions, ...
🌐
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.
🌐
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
C Examples C Real-Life Examples ... fmin(96, 2048)); Try it Yourself » · The fmin() function returns the number with the lowest value from a pair of numbers....
🌐
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?

🌐
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 ) ); }
🌐
Embarcadero
docwiki.embarcadero.com › RADStudio › Athens › en › Min
min (C++) - RAD Studio
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 the smaller of the two. Both arguments and the routine declaration must be of the same type.
Find elsewhere
🌐
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.
🌐
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.
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-3.4.3 › gcc › Min-and-Max.html
Min and Max - Using the GNU Compiler Collection (GCC)
Using GNU C++ extensions, you can write int min = i <? j; instead. Since <? and >? are built into the compiler, they properly handle expressions with side-effects; int min = i++ <? j++; works correctly.
🌐
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!
🌐
LWN.net
lwn.net › Articles › 983965
Maximal min() and max()
August 1, 2024 - One might not normally think of increased compilation time as one of them, though. It turns out that some changes to a couple of conceptually simple preprocessor macros — min() and max() — led to some truly pathological, but hidden, behavior where those macros were used.
🌐
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] ...
🌐
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 - #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"; }
🌐
ScienceDirect
sciencedirect.com › topics › engineering › min-function
Min Function - an overview | ScienceDirect Topics
FIGURE 7.4. max and min functions ... imaginary part of any value. For the min function, PTC Mathcad returns the smallest real part of any value, and i times the smallest imaginary part of any value....