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

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
🌐
Wikipedia
en.wikipedia.org › wiki › C_minor
C minor - Wikipedia
November 29, 2025 - C minor is a minor scale based on C, consisting of the pitches C, D, E♭, F, G, A♭, and B♭. Its key signature consists of three flats.
🌐
Qnx
qnx.com › developers › docs › 6.3.2 › neutrino › lib_ref › m › min.html
min()
#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; }
🌐
Guitar Tricks
guitartricks.com › chords › C-chord › min
Cmin Chord on Guitar
Here are five different ways you can play the Cmin chord on the guitar. The Cmin chord can also be called the C minor chord.
🌐
Fender
fender.com › articles › chords › how-to-play-c-sharp-minor-guitar-chord
How to Play C Sharp Minor Chord on Guitar | C#m | Fender
February 6, 2023 - The C#m chord (sometimes written as “C sharp minor” or “C# minor) is a versatile chord that can sound either happy or sad.
Find elsewhere
🌐
GtrLib
gtrlib.com › chords › Cmin
Cmin guitar chord - GtrLib Chords
September 3, 2019 - C major C minor C5 C dominant 7th C major 7th C minor 7th C minor major 7th C suspended 4th C suspended 2nd C6 C minor major 6 C9 C minor 9 C minor major 9 C11 C minor 11 C major 11 C minor major 11 C major add 9 C minor add 9 C6 add 9 C minor 6 add 9 C dominant 7th add 11 C major 7th add 11 C minor 7th add 11 C minor major 7th add 11 C dominant 7th add 13 C major 7th add 13 C minor 7th add 13 C minor major 7th add 13 C dominant 7th flat 5 C augmented C augmented 7th C dominant 7th flat 9th C dominant 7th sharp 9th C augmented 7th flat 9th C minor 7th flat 5 C minor 7th sharp 5 C minor 7th flat 9th C6 suspended 4th C6 suspended 2nd C dominant 7th suspended 4th C dominant 7th suspended 2nd C major 7th suspended 4th C major 7th suspended 2nd C diminished C diminished 7th
🌐
Piano Keyboard Guide
piano-keyboard-guide.com › key-of-c-minor.html
Chords in the key of C minor (Cm)
The following piano key chord chart shows all the triads in C minor as well as four note extended chords.
🌐
GtrLib
gtrlib.com › chords › c-minor
C minor guitar chord - GtrLib Chords
September 3, 2019 - C major C minor C5 C dominant 7th C major 7th C minor 7th C minor major 7th C suspended 4th C suspended 2nd C6 C minor major 6 C9 C minor 9 C minor major 9 C11 C minor 11 C major 11 C minor major 11 C major add 9 C minor add 9 C6 add 9 C minor 6 add 9 C dominant 7th add 11 C major 7th add 11 C minor 7th add 11 C minor major 7th add 11 C dominant 7th add 13 C major 7th add 13 C minor 7th add 13 C minor major 7th add 13 C dominant 7th flat 5 C augmented C augmented 7th C dominant 7th flat 9th C dominant 7th sharp 9th C augmented 7th flat 9th C minor 7th flat 5 C minor 7th sharp 5 C minor 7th flat 9th C6 suspended 4th C6 suspended 2nd C dominant 7th suspended 4th C dominant 7th suspended 2nd C major 7th suspended 4th C major 7th suspended 2nd C diminished C diminished 7th
🌐
C-mine
c-mine.be › en
Homepage | C-mine
Have a unique wander through 1 km of steel corridors on the C-mine plaza. Discover various spaces that allow you to experience your environment from new angles.
🌐
W3Schools
w3schools.com › c › ref_math_fmin.php
C Math fmin() Function
Create Variables Format Specifiers Change Values Multiple Variables Variable Names Real-Life Examples Code Challenge C Data Types
🌐
Kpop Wiki
kpop.fandom.com › wiki › Min_C
Min C | Kpop Wiki | Fandom
January 9, 2026 - Min C (민씨) is a British singer under Vine Entertainment. She is a member of the girl group SECRET NUMBER. On August 15, 2025, she was revealed as new member of SECRET NUMBER.[1] She made her debut with the group on August 19 with the digital ...
🌐
Fender
fender.com › articles › chords › learn-how-to-play-c-minor-guitar-chord
C Minor Guitar Chord | How to Play Cm Chord | Fender Play
February 6, 2023 - The Cm (or C minor) chord embodies an expressive softness when played. It can elicit a sobering passion, longing, or solemnity.
🌐
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 - In other words, switch from a to a language that is 45 years old (C) to one that is only about 35 years old (C++). There's a paper from 1995 that explains this in the context of min and max implemented with templates: http://www.aristeia.com/Papers/C++ReportColumns/jan95.pdf
🌐
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?

🌐
Visit Limburg
visitlimburg.be › en › what-to-do › c-mine
C-mine | Visit Limburg
This creative hotbed lives and breathes a unique atmosphere. At the former Winterslag mining site, respect for the industrial past goes hand in hand with contemporary, artistic industriousness. There is plenty to do both in and around the former ...
🌐
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 ) ); }
🌐
Songtive
songtive.com › de › chords › piano › Cmin
Cm, C-, C min, C Minor Piano Chord Chart | Songtive
Cm, C-, C min, C Minor · Notes: C, Es, G · Cm, C-, C min, C Minor (1st inversion) Notes: Es, G, C · Cm, C-, C min, C Minor (2nd inversion) Notes: G, C, Es · It is a flexible chord and scale dictionary with user libraries and a reverse mode. Songtive is based on user feedback from Piano ...
🌐
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
Practical guide on how to use the macro MIN() to find the minimum value of two variables, how it works, and the expression and formula that this macro applies.