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
The expression that is displayed by the macro MIN() is a true/false condition where a โ€œ<โ€ relational operation is applied between the โ€œaโ€ and โ€œbโ€ variables. If โ€œaโ€ is less than โ€œbโ€, โ€œaโ€ is returned. If โ€œbโ€ is less than โ€œaโ€, โ€œbโ€ is returned.
๐ŸŒ
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; }
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ cpp โ€บ c-runtime-library โ€บ reference โ€บ min
__min | Microsoft Learn
// 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 ) ); }
๐ŸŒ
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 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?

๐ŸŒ
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).
Find elsewhere
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
Quora
quora.com โ€บ What-is-int_min-in-C
What is int_min in C? - Quora
Reference: C11 Section 5.2.4.2.1. ... all capitals, not lower case, is a macro defined in limits.h. It is the minimum value you can express in the type int....
๐ŸŒ
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
๐ŸŒ
GNU
gcc.gnu.org โ€บ onlinedocs โ€บ gcc-3.4.3 โ€บ 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 ...
๐ŸŒ
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....
๐ŸŒ
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.
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ int-max-min-c-plus-plus
Using INT_MAX and INT_MIN in C/C++ | DigitalOcean
August 3, 2022 - These are actually useful macros which represent the maximum and minimum integer values. Letโ€™s take a look at it, using some examples. INT_MAX is a macro which represents the maximum integer value.
๐ŸŒ
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!
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 83153-finding-simple-min-max-mean-c.html
Finding simple min, max, mean in C - C Board
September 18, 2006 - Here's the output: enter 2 input 0 count 1 max 2 min 2 enter 5 input 0 count 2 max 5 min 2 enter 2 input 0 count 3 max 5 min 2 enter input0 count4 max5 min0 entered 3 low 0 high 5 average 3.000000 At this point the low should be 2, I'msure i'm making some stupid little mistake somewhere, it's just I don't know where.. Last edited by rQQt; 09-20-2006 at 02:45 PM. ... View Forum Posts The Richness... ... >>I'msure i'm making some stupid little mistake somewhere, it's just I don't know where.. you're making more than one little stupid mistake... ... char buf[BUFSIZ + 1]; int count=0; float input,sum; min=32356; /*missing type specifiers here*/ max=-32356; Judging by the printfs later, they're supposed to be ints.
๐ŸŒ
GNU
gcc.gnu.org โ€บ onlinedocs โ€บ gcc-3.3 โ€บ gcc โ€บ Min-and-Max.html
Minimum and Maximum Operators in C++
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 ...
๐ŸŒ
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.