Where are MIN
and MAX
defined in C, if at all?
What is the best way to implement these, as generically and type safely as possible? (Compil
@David Titarenco nailed it here, but let me at least clean it up a bit to make it look nice, and show both min()
and max()
together to make copying and pasting from here easier. :)
Update 25 Apr. 2020: I've also added a Section 3 to show how this would be done with C++ templates too, as a valuable comparison for those learning both C and C++, or transitioning from one to the other. I've done my best to be thorough and factual and correct to make this answer a canonical reference I can come back to again and again, and I hope you find it as useful as I do.
This technique is commonly used, well-respected by those who know how to use it properly, the "de facto" way of doing things, and fine to use if used properly, but buggy (think: double-evaluation side effect) if you ever pass expressions including variable assignment in to compare:
#define MAX(a,b) ((a) > (b) ? (a) : (b))
#define MIN(a,b) ((a) < (b) ? (a) : (b))
This technique avoids the above "double-evaluation" side effects and bugs, and is therefore considered the superior, safer, and "more modern" GCC C way to do this. Expect it to work with both the gcc and clang compilers, since clang is, by design, gcc-compatible (see the clang note at the bottom of this answer).
BUT: DO watch out for "variable shadowing" effects still, as statement expressions are apparently inlined and therefore do NOT have their own local variable scope!
#define max(a,b) \
({ \
__typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a > _b ? _a : _b; \
})
#define min(a,b) \
({ \
__typeof__ (a) _a = (a); \
__typeof__ (b) _b = (b); \
_a < _b ? _a : _b; \
})
Note that in gcc statement expressions, the last expression in the code block is what is "returned" from the expression, as though it was returned from a function. GCC's documentation says it this way:
The last thing in the compound statement should be an expression followed by a semicolon; the value of this subexpression serves as the value of the entire construct. (If you use some other kind of statement last within the braces, the construct has type void, and thus effectively no value.)
C++ Note: if using C++, templates are probably recommended for this type of construct instead, but I personally dislike templates and would probably use one of the above constructs in C++ anyway, as I frequently use and prefer C styles in embedded C++ as well.
This section added 25 Apr. 2020:
I've been doing a ton of C++ the past few months, and the pressure to prefer templates over macros, where able, in the C++ community is quite strong. As a result, I've been getting better at using templates, and want to put in the C++ template versions here for completeness and to make this a more canonical and thorough answer.
Here's what basic function template versions of max()
and min()
might look like in C++:
template <typename T>
T max(T a, T b)
{
return a > b ? a : b;
}
template <typename T>
T min(T a, T b)
{
return a < b ? a : b;
}
Do additional reading about C++ templates here: Wikipedia: Template (C++).
However, both max() and min() are already part of the C++ standard library, in the <algorithm>
header (#include <algorithm>
). In the C++ standard library they are defined slightly differently than I have them above. The default prototypes for std::max<>()
and std::min<>()
, for instance, in C++14, looking at their prototypes in the cplusplus.com links just above, are:
template <class T>
constexpr const T& max(const T& a, const T& b);
template <class T>
constexpr const T& min(const T& a, const T& b);
Note that the keyword typename
is an alias to class
(so their usage is identical whether you say <typename T>
or <class T>
), since it was later acknowledged after the invention of C++ templates, that the template type might be a regular type (int
, float
, etc.) instead of only a class type.
Here you can see that both of the input types, as well as the return type, are const T&
, which means "constant reference to type T
". This means the input parameters and return value are passed by reference instead of passed by value. This is like passing by pointers, and is more efficient for large types, such as class objects. The constexpr
part of the function modifies the function itself and indicates that the function must be capable of being evaluated at compile-time (at least if provided constexpr
input parameters), but if it cannot be evaluated at compile-time, then it defaults back to a run-time evaluation, like any other normal function.
The compile-time aspect of a constexpr
C++ function makes it kind-of C-macro-like, in that if compile-time evaluation is possible for a constexpr
function, it will be done at compile-time, same as a MIN()
or MAX()
macro substitution could possibly be fully evaluated at compile-time in C or C++ too. For additional references for this C++ template info, see below.
Clang note from Wikipedia:
[Clang] is designed to act as a drop-in replacement for the GNU Compiler Collection (GCC), supporting most of its compilation flags and unofficial language extensions.
I wrote this version that works for MSVC, GCC, C, and C++.
#if defined(__cplusplus) && !defined(__GNUC__)
# include <algorithm>
# define MIN std::min
# define MAX std::max
//# define TMIN(T, a, b) std::min<T>(a, b)
//# define TMAX(T, a, b) std::max<T>(a, b)
#else
# define _CHOOSE2(binoper, lexpr, lvar, rexpr, rvar) \
({ \
decltype(lexpr) lvar = (lexpr); \
decltype(rexpr) rvar = (rexpr); \
lvar binoper rvar ? lvar : rvar; \
})
# define _CHOOSE_VAR2(prefix, unique) prefix##unique
# define _CHOOSE_VAR(prefix, unique) _CHOOSE_VAR2(prefix, unique)
# define _CHOOSE(binoper, lexpr, rexpr) \
_CHOOSE2( \
binoper, \
lexpr, _CHOOSE_VAR(_left, __COUNTER__), \
rexpr, _CHOOSE_VAR(_right, __COUNTER__) \
)
# define MIN(a, b) _CHOOSE(<, a, b)
# define MAX(a, b) _CHOOSE(>, a, b)
#endif
I know the guy said "C"... But if you have the chance, use a C++ template:
template<class T> T min(T a, T b) { return a < b ? a : b; }
Type safe, and no problems with the ++ mentioned in other comments.
The maximum of two integers a
and b
is (int)(0.5((a+b)+abs(a-b)))
. This may also work with (double)
and fabs(a-b)
for doubles (similar for floats)
This is a late answer, due to a fairly recent development. Since the OP accepted the answer that relies on a non-portable GCC (and clang) extension typeof
- or __typeof__
for 'clean' ISO C - there's a better solution available as of gcc-4.9.
#define max(x,y) ( \
{ __auto_type __x = (x); __auto_type __y = (y); \
__x > __y ? __x : __y; })
The obvious benefit of this extension is that each macro argument is only expanded once, unlike the __typeof__
solution.
__auto_type
is a limited form of C++11's auto
. It cannot (or should not?) be used in C++ code, though there's no good reason not to use the superior type inference capabilities of auto
when using C++11.
That said, I assume there are no issues using this syntax when the macro is included in an extern "C" { ... }
scope; e.g., from a C header. AFAIK, this extension has not found its way info clang
There's a std::min
and std::max
in C++, but AFAIK, there's no equivalent in the C standard library. You can define them yourself with macros like
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
But this causes problems if you write something like MAX(++a, ++b)
.