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
🌐
Qnx
qnx.com › developers › docs › 6.3.2 › neutrino › lib_ref › m › min.html
min() - QNX
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; }
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
🌐
W3Schools
w3schools.com › c › ref_math_fmin.php
C Math fmin() Function
C Functions C Function Parameters C Scope C Function Declaration C Functions Challenge C Math Functions C Inline Functions C Recursion C Function Pointers
🌐
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?

🌐
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
🌐
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 ...
🌐
Linux Hint
linuxhint.com › min-function-c
MIN() Macro in C Language – Linux Hint
In this Linuxhint article, we explained everything you need to know to use the macro MIN() to find the minimum value between two variables. We looked at the syntax and definition of this macro, as well as the formula that applies a true/false condition for a “less than” operation ( < ) ...
Find elsewhere
🌐
Educative
educative.io › answers › what-is-fmin-in-c
What is fmin() in C?
fmin() is a C library function that returns the smaller numeric value of its two arguments. ... Note: An integer can also be passed as a parameter to fmin(), but will be implicitly cast to double datatype.
🌐
Cplusplus
cplusplus.com › forum › beginner › 138425
How to find max and min? (C-Program) - C++ Forum
Easiest way I can think of is to have 3 variables, a current_pancakes (or just what you have, pancakes), a pancakes_max, and a pancakes_min, initialized at -1 or something.
🌐
LWN.net
lwn.net › Articles › 983965
Maximal min() and max()
August 1, 2024 - Even if one expects a large expansion, though, the actual amount may lead to significant eyebrow elevation: the single line of code shown above expands to 47MB of preprocessor output. Bergmann explained this result this way: It nests min() multiple levels deep with the use of min3(), and each one expands its argument 20 times times now (up from 6 back in linux-6.6).
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-language › cpp-integer-limits
C and C++ Integer Limits | Microsoft Learn
July 21, 2023 - These limits are defined in the C standard header file <limits.h>. The C++ Standard Library header <limits> includes <climits>, which includes <limits.h>.
🌐
Oreate AI
oreateai.com › blog › finding-the-minimum-value-in-c › d40ed5566e064039f404da3894f00f9e
Finding the Minimum Value in C++ - Oreate AI Blog
December 22, 2025 - The min function is part of the standard library and compares two values to return the smaller one: int min(int a, int b); When using the min function, you can pass either an int or a double value; it will return the minimum value.
🌐
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.
🌐
NASA
nasa.gov › blogs › wallops › 2026 › 02 › 24 › nasa-wallops-to-support-rocket-lab-launch
NASA Wallops to Support Rocket Lab Launch - NASA
February 28, 2026 - A Rocket Lab suborbital rocket launches from Pad-0C (Launch Complex 2) on Wallops Island, Virginia.
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-3.3.4 › gcc › Min-and-Max.html
Min and Max - Using the GNU Compiler Collection (GCC)
You might then use int min = MIN (i, j); to set min to the minimum value of variables i and j. However, side effects in X or Y may cause unintended behavior. For example, MIN (i++, j++) will fail, incrementing the smaller counter twice. The GNU C typeof extension allows you to write safe macros ...
🌐
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 ) ); }
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-program-to-find-the-maximum-and-minimum-element-of-the-array
C Program to Find the Maximum and Minimum Element in the Array - GeeksforGeeks
July 12, 2025 - The simplest method to find the maximum and minimum element of the array is iterates through the array and compare each element with the assumed minimum and maximum and update them if the current element is smaller or larger respectively.
🌐
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
🌐
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
July 24, 2017 - This complexity rating is justified, ... tested independently. ... macros are evil. See the C++ FAQ: https://isocpp.org/wiki/faq/misc-technical-issues#macros-with-if which lists 4 different types of evil behavior. There are fancy hacks to try to get any particular macros such as MIN and MAX to ...