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
How Do Header Files Work in C/C++?
The convention in C/C++ when implementing a module/library is to divide it into headers (.h) and source files (.c, .cc, .cpp). You typically include only the bare minimum interface in the header file. That means things like class/struct definitions, function prototypes, and global constants. Then the implementations of said functions are placed in a corresponding source file. When making the module available to the public, you would compile the source files into binaries (often .so or .dll depending on operating system) and deliver the .h files along with the .so files. As a user of one of these libraries you need to do two things to properly use it: include the header file in your source (e.g. #include ) when compiling your code, tell the linker where to find the implementation binaries that correspond to the headers. (e.g. gcc myCoolFile.c -l myCoolOtherFile). Side note: when you install a library it typically places the .h and .so files in directories where your compiler will look for them by default (on Linux, somewhere like /usr/include and /usr/lib/) so providing just the name of the library is usually sufficient for both of the above steps. One of the benefits to packaging libraries in this way is to save compilation time. Imagine having to include the source code (.c files) for every library you used (stdio.h, stdlib.h, etc) in every project. You would need to compile each of those every time and each is potentially a massive amount of code. By using libraries as pre-compiled binaries and headers you get to save a lot of compilation time. More on reddit.com
🌐 r/learnprogramming
23
10
November 28, 2022
🌐
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; }
🌐
Ticalc
tigcc.ticalc.org › doc › stdlib.html
stdlib.h
If endptr is not NULL, strtol sets the pointer variable pointed to by endptr to point to the character that stopped the scan (i.e. *endptr = &stopper). strtol returns the value of the converted string, or 0 on error. In a case of overflow, strtol returns LONG_MAX or LONG_MIN, depending of the sign.
🌐
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 ) ); }
🌐
W3Schools
w3schools.com › c › ref_math_fmin.php
C Math fmin() Function
C Examples C Real-Life Examples ... printf("%f", fmin(96, 2048)); Try it Yourself » · The fmin() function returns the number with the lowest value from a pair of numbers....
🌐
GameDev.net
gamedev.net › forums › topic › 503081-minmax-in-c › 4282091
min/max in *c* - For Beginners - GameDev.net
July 29, 2008 - I don't believe those functions are defined in the C standard lib. In C++ they are a part of the STL. Seriously you got to roll out the big guns to write min and max:) ... Did you know that you can search the include folder to find out? If you're not sure which headers are part of the standard, you can search the internet using google to find out which headers are standard and which are not.
Find elsewhere
🌐
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!
🌐
Embarcadero
docwiki.embarcadero.com › RADStudio › Sydney › 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 ...
🌐
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 ...
🌐
Delft Stack
delftstack.com › home › howto › c max and min function
MIN and MAX Function in C | Delft Stack
October 12, 2023 - Inside the loop, we used an if statement to compare the next element of the array with the value that we saved, and we replaced its value if the next element is greater than the saved element in the case of MAX() function, and in MIN() function, we will replace the value if the next element is smaller than the saved element.
🌐
DigitalOcean
digitalocean.com › community › tutorials › int-max-min-c-plus-plus
Using INT_MAX and INT_MIN in C/C++ | DigitalOcean
August 3, 2022 - In this article, we’ll take a ... using some examples. INT_MAX is a macro which represents the maximum integer value. Similarly, INT_MIN represents the minimum integer value. These macros are defined in the header file <limits.h>, so you must include it....
🌐
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.
🌐
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.
🌐
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 ...
🌐
GNU
gnu.org › software › c-intro-and-ref › manual › html_node › Function-Header.html
Function Header (GNU C Language Manual)
Next: Function Body, Up: Example: Recursive Fibonacci [Contents][Index] In our example, the first two lines of the function definition are the header.
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-3.3 › gcc › Min-and-Max.html
Minimum and Maximum Operators in C++
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 ...
🌐
TutorialsPoint
tutorialspoint.com › cprogramming › c_header_files.htm
Header Files in C
The #include preprocessor directive is used to make the definitions of functions, constants and macros etc. from one file, usually called as a header file, available for use in another C code. A header file has ".h" extension from which you can include the forward declarations of one or more predefined functions, constants, macros etc.
🌐
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().