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
🌐
W3Schools
w3schools.com › c › ref_math_fmax.php
C Math fmax() Function
C Examples C Real-Life Examples ... printf("%f", fmax(96, 2048)); Try it Yourself » · The fmax() function returns the number with the highest value from a pair of numbers....
Discussions

Math.min(Math.max(num, min), max)
I find the following easier to read : · Math.min(Math.max(num, lower_bound), upper_bound) More on news.ycombinator.com
🌐 news.ycombinator.com
271
238
August 22, 2020
Add integer functions min and max to C language?
C is not a batteries included language. These are extremely easy to implement using a function or macro. Literally one-liners that are impossible to screw up. The reason why fmin et al exist is that there are eccentricities to floating point types and it's not a simple comparison. For example, is 7 > NAN? More on reddit.com
🌐 r/c_language
4
2
July 30, 2023
Is max() predefined or not ?
It's not a standard library function or macro. Some implementations may provide it. Presumably QNX does. But I bet you're not using QNX... so you probably shouldn't look at that documentation. More on reddit.com
🌐 r/C_Programming
12
7
April 23, 2023
simple c++ question regarding std::max()
std::max doesn't assign a value. It returns one. More on reddit.com
🌐 r/cpp_questions
52
13
August 13, 2023
🌐
Reddit
reddit.com › r/c_programming › is max() predefined or not ?
r/C_Programming on Reddit: Is max() predefined or not ?
April 23, 2023 -

Sorry for the newbie question, I'm looking for a way to find the maximum between two values in C and I stumbled upon this, which make it sound like max() is a macro defined in stdlib.h, however when I look up the file I can't find it, and when I try to compile the example I get an 'implicit declaration' warning then an 'undefined reference' at execution.

That stackoverflow discussion seems to go that way and suggests that one uses fmax from the math library, or write the max() themselves.

I don't know if I'm just bad at reading documentation or if there's actually something special about that I should know about. Either way if someone can help me figuring this out, i'd be grateful.

(PS : it's for a school project where I won't have any issues with double evaluation so a dirty solution will do)

🌐
Cplusplus
cplusplus.com › reference › algorithm › max
std::max
Returns the largest of a and b. If both are equivalent, a is returned. The versions for initializer lists (3) return the largest 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.
🌐
Quora
quora.com › What-is-the-purpose-of-the-max-function-in-C-programming
What is the purpose of the 'max' function in C programming? - Quora
Learning C (programming l... ... UG in Bachelor of Engineering Degrees & Biomedical Engineering, Karpagam Academy of Higher Education (Graduated 2024) · 2y · The "max" function in C compares two integers and returns the larger value.
🌐
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.
Find elsewhere
🌐
GNU
gcc.gnu.org › onlinedocs › gcc-3.4.3 › 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 ...
🌐
TutorialsPoint
tutorialspoint.com › article › c-program-to-find-maximum-of-four-integers-by-defining-function
C program to find maximum of four integers by defining function
July 24, 2017 - So, if the input is like a = 5, b = 8, c = 2, d = 3, then the output will be 8 ... #include <stdio.h> int max(int x, int y){ if(x > y){ return x; }else{ return y; } } int main(){ int a = 5, b = 8, c = 2, d = 3; int left_max = max(a, b); int right_max = max(c, d); int final_max = max(left_max, right_max); printf("Maximum number is: %d", final_max); }
🌐
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; }
🌐
7-Zip Documentation
documentation.help › C-Cpp-Reference › max.html
max - C/C++ Reference - Documentation & Help
The max() function returns the greater of x and y. If the binary predicate p is given, then it will be used instead of the < operator to compare the two elements.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › how-to-find-maximum-value-in-an-array-in-c
How to Find Maximum Value in an Array in C? - GeeksforGeeks
July 23, 2025 - We can find the maximal value in an array by taking a variable max and repeatedly comparing it to the elements of the array using loops.
🌐
Delft Stack
delftstack.com › home › howto › c max and min function
MIN and MAX Function in C | Delft Stack
October 12, 2023 - See the code below. #include <stdio.h> #define MIN(i, j) (((i) < (j)) ? (i) : (j)) #define MAX(i, j) (((i) > (j)) ?
🌐
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!
🌐
Codeforwin
codeforwin.org › home › c program to find maximum and minimum using functions
C program to find maximum and minimum using functions - Codeforwin
September 14, 2023 - How to find maximum and minimum of two or more numbers using functions in C programming.
🌐
GeeksforGeeks
geeksforgeeks.org › c++ › stdmax-in-cpp
std::max in C++ - GeeksforGeeks
January 11, 2025 - We can use std::max() function to find the larger among two values.
🌐
GeeksforGeeks
geeksforgeeks.org › c language › c-program-to-find-the-maximum-and-minimum-element-of-the-array
C Program to Find the Maximum and Minimum Element in the Array - GeeksforGeeks
July 12, 2025 - The simplest method to find the maximum and minimum element of the array is iterates through the array and compare each element with the assumed minimum and maximum and update them if the current element is smaller or larger respectively.
🌐
Educative
educative.io › answers › how-to-use-the-max-function-in-cpp
How to use the max() function in C++
The max() function in C++ accepts two values and returns the larger one. This function is available in <algorithm.h>. The max() function helps during coding contests when you want to find the maximum of two values in the logic.
🌐
Cppreference
en.cppreference.com › w › cpp › algorithm › max.html
std::max - cppreference.com
December 5, 2024 - int n = -1; const int& r = std::max(n + 2, n * 2); // r is dangling
🌐
Hacker News
news.ycombinator.com › item
Math.min(Math.max(num, min), max) | Hacker News
August 22, 2020 - I find the following easier to read : · Math.min(Math.max(num, lower_bound), upper_bound)