Article View: comp.lang.c
Article #703Re: variadic min and max, (any some and none, cat and more)
From: bartc
Date: Fri, 23 Oct 2026 10:37
Date: Fri, 23 Oct 2026 10:37
84 lines
3994 bytes
3994 bytes
On 23/10/2026 10:07, David Brown wrote:
> 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
> 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,
At last somebody who knows what ABI means.
> 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.
For this purpose, where all elements are expected to be the same type,
then passing a list is more appropriate, and more type-safe. Especially
when 'min_func' has a specific return type anyway.
Example:
#include <stdio.h>
#include <limits.h>
int min(int* list) {
enum {sentinel = 0};
int result = INT_MAX;
while (*list != sentinel) {
if (*list < result) result = *list;
++list;
}
return result;
}
int main(){
printf("%d\n", min((int[]){3,1,4,1,5,9,2,6,0}));
}
Although a tidier way of imparting the length is better. It depends on
whether the common use-case is a small fixed number of elements, or an
arbitrary array (where the length is likely in some variable.)
An even simpler way where the number of elements is small and is known,
is to just write dedicated functions such as min2(x, y) and min3(x, y,
z). The first will be by far the most common need IME.
Technical Details
Message-ID:
<11bfdcs$1mvrv$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> <11bf84v$1jkb5$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: bartc <bc@freeuk.com> Newsgroups: comp.lang.c Subject: Re: variadic min and max, (any some and none, cat and more) Date: Fri, 23 Oct 2026 11:37:15 +0100 Organization: A noiseless patient Spider Lines: 84 Message-ID: <11bfdcs$1mvrv$1@dont-email.me> References: <11bes8b$1gncr$1@dont-email.me> <11bf84v$1jkb5$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 10:37:16 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1802111"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19prQSl7I5TGkDErmXmP/p5"; posting-host="df1406970d80d921c8a9b174013cabf0" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:TkV/v/Yg399Zl9VxG0ukhz6ROqQ= sha256:qh9QH/5+aRthPdvO4GgDem2RgTQ3v7qxiZrrUbRQaHU= sha1:cbiTK4qL+x4BCo6Nd+hOE4Js8hA= sha256:Lkr8uzZxM6vehjvyWuvFNPvxMOO/+K46fyARiGKsUXw= In-Reply-To: <11bf84v$1jkb5$1@dont-email.me> Content-Language: en-GB