Article View: comp.lang.c
Article #700Re: variadic min and max, (any some and none, cat and more)
From: David Brown
Date: Fri, 23 Oct 2026 09:07
Date: Fri, 23 Oct 2026 09:07
145 lines
5971 bytes
5971 bytes
On 23/10/2026 07:44, fir wrote:
> when thinkong on variadic functions not much come to mind but
> only recently i noticed min or max would be good for variadic
>
> the ai wrote me such variadic min
>
> #include <stdarg.h>
>
> int Min(int count,...)
> {
> va_list args;
> va_start(args,count);
>
> ```
> int min=va_arg(args,int);
> for(int i=1;i<count;i++)
> {
> int x=va_arg(args,int);
> if(x<min) min=x;
> }
>
> va_end(args);
> return min;
> }
>
>
> to use
>
> int a=Min(4,10,3,7,-2); // -2
> int b=Min(3,15,8,12); // 8
> int c=Min(1,42); // 42
>
>
> problem is imo this leading argument n, imo it should
>
> just be called
>
> min(34,34,2);
>
> thus the variadic support probably need to change
>
>
> imo it would be better if compiler would jut provide for user such
> things as
>
>
> *arg - (or *args?) (the ram block of arguments with size sizeof(*arg))
>
> afg[0], arg[1], arg[2] - arguments vakues
>
> arg - pointer to *arg
>
> arg_n - how many
>
>
Variadic functions have no way of counting their arguments, or
identifying their types. This can't be changed easily - it is built
into the way C functions are called (in pretty much every
implementation). The caller simply puts the arguments into registers or
onto the stack according to the ABI for the target, then calls the
function. The function has to /know/ the number and type of the
arguments - that cannot be determined automatically.
This knowledge usually comes from one of two sources. One is from the
first argument - such as you use above, providing the number of
arguments explicitly, or the format argument for "printf". The second
is by using a sentinel - a special end value.
Variadic functions are naturally very fragile, and the compiler cannot
help spot mismatches (except in well-known cases, like printf).
So it is not a case of "variadic function support needs to change" - it
simply cannot be fixed within the C function model.
Variadic macros are in some ways better, though they have their own
challenges and limitations. In particular, you can't loop, they can't
be recursive, you can't count the number the number of arguments
directly, and solutions to these evaluate the arguments multiple times.
But with enough effort, it is possible to handle this - there are plenty
of blogs with creative solutions. (Jens Gustedt has several good articles.)
If I wanted to write such a variadic minimum function, I would do so
using a sentinel, with a macro to add the sentinel:
#define SENTINEL INT_MIN
#define min(x0, ...) min_func(x0 __VA_OPT__(,) __VA_ARGS__, SENTINEL)
int min_func(int x0, ...) {
va_list args;
va_start(args);
int m = x0;
while (true) {
int x = va_arg(args, int);
if (x == SENTINEL) return m;
if (x < m) m = x;
}
}
It is up to the user to ensure that the arguments to "min" are all ints,
and none of them match the sentinel value.
This code uses a C23 feature __VA_OPT__ which can be avoided using a gcc
extension or some more complex macros. It also uses the C23 feature of
va_start() without additional arguments. If you don't want C23, you can
therefore use :
#define min(x0, ...) min_func(x0, ## __VA_ARGS__, SENTINEL)
va_start(args, x0);
But the C23 features make it slightly neater.
None of this means I think a variadic function here is a good idea. A
full variadic macro solution is probably better, but somewhat messy,
complex (when you haven't done it before), and always has a fixed limit
on the maximum number of elements.
A good solution could not be done by changing variadic functions in C
without a massive and disruptive change to the function model. It
cannot be done by changing variadic macros in C without a massive and
disruptive change to the macro model. It would need something else - a
variadic template solution that is always expanded inline (somewhat like
a macro), but is not text-based like pre-processor macros so that you
can get recursion or loops. Adding that to C would be a huge job - so
you either stick to the limitations of variadic macros in C, or switch
to C++.
template <typename T>
constexpr auto min(T x0) { return x0; }
template <typename T, typename U>
constexpr auto min(T x0, U x1) { return (x0 < x1) ? x0 : x1; }
template <typename T, typename U, typename... Args>
constexpr auto min(T x0, U x1, Args... args) {
return min(x0, min(x1, args...));
}
(Here you can use any types that make sense, not just int.)
Technical Details
Message-ID:
<11bf84v$1jkb5$1@dont-email.me>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mailReferences:
<11bes8b$1gncr$1@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: David Brown <david.brown@hesbynett.no> Newsgroups: comp.lang.c Subject: Re: variadic min and max, (any some and none, cat and more) Date: Fri, 23 Oct 2026 11:07:43 +0200 Organization: A noiseless patient Spider Lines: 145 Message-ID: <11bf84v$1jkb5$1@dont-email.me> References: <11bes8b$1gncr$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Fri, 23 Oct 2026 09:07:44 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1692005"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX199bOuAKtev3iy2EuL6h/CG45XGkndxrbU="; posting-host="7400fcdb32ce88f15c58e9bcb96ebcf6" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:cmyJtOpN0aevR/PcXKYb9X+yc88= sha256:fzE8tnEW3fVXCG25w9WYDXo6RpnXoIY80t+tn1VGmzg= sha1:86+nllE3Xa2onMvbn/OkgXIOEAo= sha256:H6KczL5qbR0Z8RHpM7+RwR9FURFjI4MkdD2breYcf7c= Content-Language: en-GB, nb-NO In-Reply-To: <11bes8b$1gncr$1@dont-email.me>