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....
🌐
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)

🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › system.math.max
Math.Max Method (System) | Microsoft Learn
Public Shared Function Max (val1 As UInteger, val2 As UInteger) As UInteger · val1 · UInt32 · The first of two 32-bit unsigned integers to compare. val2 · UInt32 · The second of two 32-bit unsigned integers to compare. UInt32 · Parameter val1 or val2, whichever is larger. Attributes · CLSCompliantAttribute · Source: Math.cs ·
🌐
Cppreference
en.cppreference.com › w › cpp › algorithm › max.html
std::max - cppreference.com
int n = -1; const int& r = std::max(n + 2, n * 2); // r is dangling
🌐
Arduino
docs.arduino.cc › language-reference › en › functions › math › max
max()
Arduino programming language can be divided in three main parts: functions, values (variables and constants), and structure · For controlling the Arduino board and performing computations
🌐
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
Answer (1 of 4): Same as max function in any language. Except C will be much more primitive. a more sophisticated generic rendering would be: max (collection [comparable]): comparable this says that we can take any collection where all the elements are comparable and find the value that is the...
🌐
Qnx
qnx.com › developers › docs › 6.5.0SP1.update › com.qnx.doc.neutrino_lib_ref › m › max.html
max
Return the greater of two numbers · The max() macro returns the greater of two values
Find elsewhere
🌐
Codecademy
codecademy.com › docs › c# › math functions › .max()
C# (C Sharp) | Math Functions | .Max() | Codecademy
April 29, 2023 - Math.Max() returns the greater value from num1 and num2. The following example compares two integers i1 and i2 and writes the greater integer to the console.
🌐
Cplusplus
cplusplus.com › reference › algorithm › max
std::max - algorithm
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.
🌐
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.
🌐
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 ...
🌐
Embarcadero
docwiki.embarcadero.com › Libraries › Sydney › en › System.Math.Max
System.Math.Max - RAD Studio API Documentation
Up to Parent: System.Math · Delphi ... Extended; C++ extern DELPHI_PACKAGE int __fastcall Max(const int A, const int B)/* overload */; Returns the greater of two numeric values....
🌐
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
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); }
🌐
Math.js
mathjs.org › docs › reference › functions › max.html
math.js
In case of a multidimensional array, the maximum of the flattened array will be calculated. When dim is provided, the maximum over the selected dimension will be calculated. Parameter dim is zero-based. math.max(a, b, c, ...) math.max(A) math.max(A, dimension) Type | Description —- | ———– ·
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Math.min and Math.max methods with JS
May 23, 2019 - I first wrote this code: let numbers = (10, 15, 59, 20); let a = Math.max(numbers); console.log(a); …which display 20 in the console. I’ve noticed that it always returns the last number of the numbers variable. Then, i wrote this (I’ve found it on W3S and modify it a bit): function myFunction() { var a = Math.min(5, 10); var b = Math.min(0, 150, 30, 20, 38); var c = Math.min(-5, 10); var d = Math.min(-5, -10); var e = Math.min(1.5, 2.5); console.log(a + " " + b + " " + ...
🌐
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; }
🌐
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)
🌐
IronPDF
ironpdf.com › ironpdf blog › .net help › math.max c#
math.max C# (How It Works For Developers)
June 21, 2025 - One such function, the Math.Max C# method, allows programmers to determine the maximum value between two numbers, a common requirement in many applications.
🌐
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
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!