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
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ cpp โ€บ c-runtime-library โ€บ reference โ€บ min
__min | Microsoft Learn
October 26, 2022 - ... The smaller of the two arguments. The __min macro compares two values and returns the value of the smaller one. The arguments can be of any numeric data type, signed or unsigned.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ min-function-c
MIN() Macro in C Language โ€“ Linux Hint
The macro MIN() returns the minimum value between the โ€œaโ€ and โ€œbโ€ variables. 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โ€ ...
๐ŸŒ
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 - 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(). If it's there, use the stuff from your compiler vendor instead of writing it yourself! If your compiler doesn't have integer min and max, or you are worried about breaking existing macro code, convert the macros into inline functions with minimal changes to your code base:
๐ŸŒ
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.
๐ŸŒ
w3resource
w3resource.com โ€บ c-programming-exercises โ€บ c-snippets โ€บ c-program-to-define-macro-to-find-maximum-minimum-of-two-numbers.php
C โ€“ Macro to find maximum/minimum of two numbers
In computer programming, a macro is a rule or pattern that specifies how a certain input should be mapped to a replacement output. Applying a macro to an input is known as macro expansion. ... # include <stdio.h> #define MAX(x, y) ((x) > (y) ? (x) : (y)) #define MIN(x, y) ((x) < (y) ?
๐ŸŒ
Codeforwin
codeforwin.org โ€บ home โ€บ c program to find maximum or minimum using macro
C program to find maximum or minimum using macro - Codeforwin
July 20, 2025 - Lets define two macro that accepts two arguments say MAX(x, y) and MIN(x, y). It will return maximum or minimum number respectively. For this exercise we will use conditional (ternary) operator to find maximum or minimum.
๐ŸŒ
Dustri
dustri.org โ€บ b โ€บ min-and-max-macro-considered-harmful.html
MIN and MAX macro considered harmful
February 17, 2015 - Long story short: Don't use macro for MIN and MAX in C, because you'll use them one day with a side-effect argument, and then, Smokey, my friend, you will be entering a world of pain.
๐ŸŒ
LWN.net
lwn.net โ€บ Articles โ€บ 983965
Maximal min() and max()
August 1, 2024 - Rather than try to fix the existing complex macros, he just added a couple more with a familiar look to them: /* * Use these carefully: no type checking, and uses the arguments * multiple times. Use for obvious constants only. */ #define CONST_MIN(a,b) ((a)<(b)?(a):(b)) #define CONST_MAX(a,b) ((a)>(b)?(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 - The MIN and MAX functions are used to find the minimum and maximum number from two values and are not predefined in C language. If we want to use the MIN and MAX functions, we must define them in C. We can use macros to define the MIN and MAX functions in the C language.
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ cplusplus-programming โ€บ 21156-min-max-macros.html
Min and Max Macros
July 7, 2002 - min and max macro's compare two values and return either the smaller or larger value respectively. It compares numerical data types either signed or unsigned. The prototypes look like this. #define min(a, b) (((a) < (b)) ? (a) : (b)) #define max(a, b) (((a) > (b)) ?
๐ŸŒ
Narkive
openwatcom.users.c-cpp.narkive.com โ€บ QvDdj60Z โ€บ max-min-macros-are-not-been-declared-in-stdlib-h-for-c
max/min macros are not been declared in stdlib.h for C++
Chapin ยท Post by Evgeny Kotsuba Max/min macros are used for years....And is very simple, how it can causes any problem I can't imagine. Min and max macros are not part of the C standard (that I could see... did I miss it?). Thus a portable C program should provide its own version of those macros.
๐ŸŒ
Arduino Forum
forum.arduino.cc โ€บ community โ€บ suggestions for the arduino project
min, max, abs.. are macroses? - Suggestions for the Arduino Project - Arduino Forum
December 23, 2011 - Why on the earth these functions are defined as macros: #define min(a,b) ((a) =0?(long)((x)+0.5):(long)((x)-0.5)) #define radians(deg) ((deg)*DEG_TO_RAD) #define degrees(rad) ((rad)*RAD_TO_DEG) #define sq(x) ((x)*(x)) This is dangerous and leads ...
๐ŸŒ
GitHub
gist.github.com โ€บ jorgenpt โ€บ 56a81d0a04ef6bf73177
An attempt at some saner min-max-clamp macros for C and Objective-C. Require a modern compiler (C11) with support for statement expressions (Gnu11). ยท GitHub
An attempt at some saner min-max-clamp macros for C and Objective-C. Require a modern compiler (C11) with support for statement expressions (Gnu11). - SaneMinMaxMacros.h
๐ŸŒ
Hacker News
news.ycombinator.com โ€บ item
Maximal min() and max() | Hacker News
August 12, 2024 - The macros now do type-safe comparisons that work correctly with combinations of different argument types where this is possible, work in a constant context (eg defining array bounds), work correctly in the face of implicit type coercion and include a 3-way min and max that have these same ...
๐ŸŒ
GNU
gcc.gnu.org โ€บ onlinedocs โ€บ cpp โ€บ Duplication-of-Side-Effects.html
Duplication of Side Effects (The C Preprocessor)
The best solution to this problem is to define min in a way that computes the value of foo (z) only once. The C language offers no standard way to do this, but it can be done with GNU extensions as follows:
๐ŸŒ
GNU
gcc.gnu.org โ€บ onlinedocs โ€บ cpp โ€บ Macro-Arguments.html
Macro Arguments (The C Preprocessor)
This rule may seem strange, but it is carefully designed so you need not worry about whether any function call is actually a macro invocation. You can run into trouble if you try to be too clever, though. See Argument Prescan, for detailed discussion. For example, min (min (a, b), c) is first ...
๐ŸŒ
Microsoft Learn
learn.microsoft.com โ€บ en-us โ€บ windows โ€บ win32 โ€บ multimedia โ€บ min
min macro (Minwindef.h) - Win32 apps | Microsoft Learn
December 11, 2020 - The min macro compares two values and returns the smaller one. The data type can be any numeric data type, signed or unsigned. The data type of the arguments and the return value is the same.