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

Top answer
1 of 16
549

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
131

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
Discussions

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
Max in a list and its index in Haskell — in the case of a tie, I need the element closest to the head

You are using comparing fst, meaning you are only comparing the elements. If there is more than one element in your zipped list which has the maximum value according to that comparator, I think maximumBy is morally allowed to return any of them. So I would simply use a different comparator: one which, in case of a tie between the fsts, compares the indices and considers the smaller indices to be better.

More on reddit.com
🌐 r/haskell
8
0
October 24, 2020
Function in curve mode.. Why can't I make a curve?

I tried that with mac (using the alt option key) and is working perfect. double check in the inspector of the "function" object that the mode indeed changed to 'Curve'

More on reddit.com
🌐 r/MaxMSP
12
6
March 1, 2021
How to control a function object in curve mode inside a poly?
Can you post your patch? More on reddit.com
🌐 r/MaxMSP
5
3
July 19, 2020
🌐
W3Schools
w3schools.com › c › ref_math_fmax.php
C Math fmax() Function
The fmax() function is defined in the <math.h> header file. Tip: Use the fmin() function to return the number with the lowest value. ... Coding fundamentals as bite-sized lessons and challenges.
🌐
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...
🌐
Cplusplus
cplusplus.com › reference › algorithm › max
std::max
Binary function that accepts two values of type T as arguments, and returns a value convertible to bool. The value returned indicates whether the element passed as first argument is considered less than the second. The function shall not modify any of its arguments.
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "c howtos" › "min and max function in c"
MIN and MAX Function in C | Delft Stack
October 12, 2023 - Inside the MAX() and MIN() function, we stored the first element of the array in a variable, and then we compared it with all the other elements of the array using a loop that will stop when the integer i becomes equal to the length of the array which means the loop has reached the end of the array.
🌐
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
March 15, 2026 - #include <stdio.h> int max(int x, int y) { return (x > y) ? x : y; } int main() { int a = 10, b = 25, c = 15, d = 8; int maximum = max(max(a, b), max(c, d)); printf("Numbers: a=%d, b=%d, c=%d, d=%d<br>", a, b, c, d); printf("Maximum number is: %d<br>", maximum); return 0; } Numbers: a=10, b=25, c=15, d=8 Maximum number is: 25 · The max() function can be reused for any number comparison
Find elsewhere
🌐
Qnx
qnx.com › developers › docs › 6.5.0SP1.update › com.qnx.doc.neutrino_lib_ref › m › max.html
max
The max() macro returns the greater of two values. #include <stdio.h> #include <stdlib.h> int main( void ) { int a; a = max( 1, 10 ); printf( "The value is: %d\n", a ); return EXIT_SUCCESS; }
🌐
Codeforwin
codeforwin.org › home › c program to find maximum and minimum using functions
C program to find maximum and minimum using functions - Codeforwin
July 20, 2025 - Let us define function to find maximum. First give a meaningful name to our function. Say max() function is used to find maximum between two numbers. Second, we need to find maximum between two numbers.
🌐
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....
🌐
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 2, 2022 - Are std::min and std::max better than fmin and fmax in C++? Do they provide essentially the ... C math.h header according to the C standard (C99).
🌐
PJA University
users.pja.edu.pl › ~jms › qnx › help › watcom › clibref › src › max.html
max, __max
The C max() and C++ __max() macros evaluate to be the greater of two values. The larger of the two values. #include <stdio.h> #include <stdlib.h> void main() { int a; /* * The following line will set the variable "a" * to 10, since 10 is greater than 1.
🌐
Ticalc
tigcc.ticalc.org › doc › stdlib.html
stdlib.h
max is an inline function (implemented using GNU C smart macros) which returns the greater 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 Man Pages
man7.org › linux › man-pages › man3 › max.3.html
MAX(3) - Linux manual page
MAX(3) Library Functions Manual MAX(3) MAX, MIN - maximum or minimum of two values · Standard C library (libc, -lc) #include <sys/param.h> MAX(a, b); MIN(a, b); These macros return the maximum or minimum of a and b. These macros return the value of one of their arguments, possibly converted ...
🌐
w3resource
w3resource.com › c-programming-exercises › variadic › c-variadic-exercise-3.php
C Variadic function - Maximum and minimum of a variable number of integers
November 1, 2025 - In the above code, the max() and min() functions take a variable number of arguments using the ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › cpp › c-runtime-library › reference › max
__max | Microsoft Learn
October 26, 2022 - Access to this page requires authorization. You can try changing directories. ... A preprocessor macro that returns the larger of two values. ... The __max macro compares two values and returns the value of the larger one.
🌐
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 - Initialize a variable maxVal with value arr[0]. Traverse through the whole array. During traversal: If maxVal is less than the current value of array update the value of maxVal.
🌐
C For Dummies
c-for-dummies.com › blog
Min and Max | C For Dummies Blog
September 1, 2013 - array_chop() This function (actually a family of functions) slices, dices, splits and spits up an array in various ways. array_max() This function returns the highest (maximum) value of all the values stored in the array.
🌐
Educative
educative.io › answers › how-to-use-the-max-function-in-cpp
How to use the max() function in C++
In other words, you can pass a function that returns a Boolean value that denotes which value to pick out of the two given values. The max() function returns the maximum value from the two given values.
🌐
LWN.net
lwn.net › Articles › 983965
Maximal min() and max()
The hazards that come with a macro like this, such as the double evaluation of the arguments, were pointed out in the text. Still, that did not prevent kernel developers from making use of it; as covered here in 2001, there were over 150 definitions of min() and max() matching the above pattern in the 2.4.8 kernel.