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
🌐
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; }
🌐
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?

🌐
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 example, we create the “a” and “b” variables of type “int” to which we assign an arbitrary value and from which we find the minimum by calling the macro MIN(). We then output the returned value using the printf() function. To do this, we include the “stdio.h” and “param.h” headers and open a main() function of type void.
🌐
Cplusplus
cplusplus.com › reference › algorithm › min
std::min
Note that invalid arguments cause undefined behavior. max · Return the largest (function template) min_element · Return smallest element in range (function template) Home page | Privacy policy © cplusplus.com, 2000-2026 - All rights reserved - v3.3.4s Spotted an error?
🌐
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 ) ); }
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › c max and min function
MIN and MAX Function in C | Delft Stack
October 12, 2023 - For example, let’s define the MIN and MAX functions using C language macros. See the code below. #include <stdio.h> #define MIN(i, j) (((i) < (j)) ? (i) : (j)) #define MAX(i, j) (((i) > (j)) ?
🌐
Linux Hint
linuxhint.com › min-and-max-in-c
Min and Max in C – Linux Hint
Then the ‘fmin’ and ‘fmax’ functions are called and the range or subset of the array is passed to them. The ‘fmin’ and ‘fmax’ functions return the minimum or maximum elements respectively. An example of such a type of function is shown below: #include<stdio.h> #include<math.h> int main() { printf("fmax(223, 422) = %f\n", fmax(223, 422)); printf("fmin(9.9, 2.8) = %f\n", fmin(9.9, 2.8)); return 0; }
🌐
Embarcadero
docwiki.embarcadero.com › RADStudio › Athens › en › Min
min (C++) - RAD Studio
Both arguments and the routine declaration must be of the same type. Return Value · min returns the smaller of two values. Example · #include <stdlib.h> #include <stdio.h> #ifdef __cplusplus int max (int value1, int value2); int max(int value1, int value2) { return ( (value1 > value2) ?
🌐
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"; }
🌐
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 - We can use std::min() function to find the smaller elements between the two elements. It uses < operator for comparison. ... This function returns the smaller of the two values. If both are equal, returns the first value.
🌐
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....
🌐
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 - 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().
🌐
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 ...
🌐
Educative
educative.io › answers › how-to-use-min-function-in-cpp
How to use min() function in C++
In other words, you can pass a function that returns a Boolean value that denotes which value to pick out of the two given values. The min() function returns the minimum value from the two given values.
🌐
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 - Write a C program to compute the minimum of two integers using inline function. ... #include <stdio.h> inline int min(int x, int y) { return (x < y) ?
🌐
ScienceDirect
sciencedirect.com › topics › engineering › min-function
Min Function - an overview | ScienceDirect Topics
Figure 6.4. max and min functions ... the largest imaginary part of any value. For the min function, Mathcad returns the smallest real part of any value, and i times the smallest imaginary part of any value....