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
🌐
W3Schools
w3schools.com › c › ref_math_fmax.php
C Math fmax() Function
C Functions C Function Parameters C Scope C Function Declaration C Functions Challenge C Math Functions C Inline Functions C Recursion C Function Pointers
🌐
W3Schools
w3schools.com › cpp › cpp_math.asp
C++ Math
The max(x,y) function can be used to find the highest value of x and y: cout << max(5, 10); Try it Yourself » · And the min(x,y) function can be used to find the lowest value of x and y: cout << min(5, 10); Try it Yourself » ·
🌐
W3Schools
w3schools.com › cs › cs_math.php
C# Math
The Math.Max(x,y) method can be used to find the highest value of x and y: ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
🌐
W3Schools
w3schools.com › jsref › jsref_max.asp
JavaScript Math max() Method
The Math.max() method returns the number with the highest value.
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 › cs › trycs.php
W3Schools online CS editor
❯Run Code Ctrl+Alt+R Change Orientation Ctrl+Alt+O Change Theme Ctrl+Alt+D Go to Spaces Ctrl+Alt+P
🌐
W3Schools
w3schools.com › python › ref_func_max.asp
Python max() Function
Built-in Modules Random Module ... Python Certificate Python Training ... The max() function returns the item with the highest value, or the item with the highest value in an iterable....
🌐
W3Schools
w3schools.com › c › c_ref_math.php
C math (math.h) Library Reference
C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Compiler C Syllabus C Study Plan C Interview Q&A C Certificate ... The <math.h> library has many functions that allow you to perform mathematical tasks on numbers. ... If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com
Find elsewhere
🌐
W3Schools
w3schools.com › java › ref_math_max.asp
Java Math max() Method
System.out.println(Math.max(2.0, ... 22)); System.out.println(Math.max(96L, 2048L)); ... The max() method returns the number with the highest value from a pair of numbers....
🌐
W3Schools Blog
w3schools.blog › home › javascript math max() method
JavaScript Math max() method - W3schools
May 28, 2019 - The JavaScript math max() method is used to get the maximum value from a set of numbers. Syntax: ... Parameters n1,n2,…: It represents the set of numbers. Returns Maximum value from a set of numbers. ... The maximum number of views of this element is reached. Please contact the webmaster ...
🌐
Cppreference
en.cppreference.com › w › cpp › algorithm › max.html
std::max - cppreference.com
Returns the greater of the given values · The signature of the comparison function should be equivalent to the following:
🌐
W3Schools
w3schools.com › php › func_math_max.asp
PHP max() Function
max(array_values); or max(value1,value2,...); ❮ PHP Math Reference · ★ +1 · Sign in to track progress · REMOVE ADS · PLUS · SPACES · GET CERTIFIED · FOR TEACHERS · BOOTCAMPS · CONTACT US · × · If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail: sales@w3schools.com ·
🌐
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 - 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.
🌐
Cach3
w3schools.com.cach3.com › cs › cs_math.asp.html
C# Math - W3Schools
The C# Math class has many methods that allows you to perform mathematical tasks on numbers. The Math.Max(x,y) method can be used to find the highest value of x and y:
🌐
W3Schools
w3schools.com › js › tryit.asp
W3Schools online HTML editor
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
W3Schools
w3schools.com › js › tryit.asp
JavaScript Math.max()
The W3Schools online code editor allows you to edit code and view the result in your browser
🌐
W3Schools
w3schools.com › jsref › jsref_max_value.asp
W3Schools.com
MAX_VALUE is a property of the JavaScript Number object. You can only use it as Number.MAX_VALUE.