🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #704

Re: variadic min and max, (any some and none, cat and more)

#704
From: David Brown
Date: Fri, 23 Oct 2026 11:49
129 lines
5708 bytes
On 23/10/2026 12:37, bartc wrote:
> 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.

Note that an "ABI" encompasses many things - calling convention(s) are 
just one (albeit important) part of it.

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

It could be combined with a macro :

int min_list(size_t n, const int * list) {
     int m = list[0];
     for (size_t i = 1; i < n; i++) {
         if (list[i] < m) m = list[i];
     }
     return m;
}

#define min(...) min_list( \
	sizeof((int []) { __VA_ARGS__ }) / sizeof(int), \
         int []) { __VA_ARGS__ })

Then it can be called with just :

         printf("%d\n", min(3, 1, 4, 1, 5, 9, 2, 6, 0));

Not a bad solution, when a single fixed type will do.

> 
> 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.
> 

You can take this a step further - here using macros so that it works 
for any suitable type :

#define min2(a, b) (((a) < (b)) ? (a) : (b))
#define min3(a, b, c) min2(a, min2(b, c))
#define min4(a, b, c, d) min2(min2(a, b), min2(c, d))
#define min5(a, b, c, d, e) min2(min2(a, b), min3(c, d, e))
#define min_arg(a, b, c, d, e, ...) min5(a, b, c, d, e)
#define min(a, ...) min_arg(__VA_ARGS__ __VA_OPT__(,) a, a, a, a, a, a)

The first 5 are just "normal" fixed argument minX macros of different 
sizes.  Then we make a variadic macro "min" that passes on the arguments 
it is given, with extra copies of "a" so that min5 can always be called, 
along with an intermediary "min_arg" to absorb any unnecessary extra 
arguments.

(Again, I am not saying I recommend any of this, but it can be fun to 
play around with.)
Technical Details
Message-ID: <11bfhl3$1ojh8$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-mail
References: <11bes8b$1gncr$1@dont-email.me> <11bf84v$1jkb5$1@dont-email.me> <11bfdcs$1mvrv$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 13:49:55 +0200
Organization: A noiseless patient Spider
Lines: 129
Message-ID: <11bfhl3$1ojh8$1@dont-email.me>
References: <11bes8b$1gncr$1@dont-email.me> <11bf84v$1jkb5$1@dont-email.me>
 <11bfdcs$1mvrv$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 11:49:56 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="1855016"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/4OisR06Nkjq1fKDpjMwFgKFxDu5c0e2k="; posting-host="7400fcdb32ce88f15c58e9bcb96ebcf6"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:1re957O3RpnmNqA/Ce18C2MToLE= sha256:u1NCBd5hpKisDkv5dlBnXvDmuHpmClRrCU0GkvSqZiU=
	sha1:0XYb8oWM1cZxVzU+zg/q1UzYxQU= sha256:ekDdDBQCXnvnmOMBeVNATELagjgm4QfpFKbUQGrRqIk=
In-Reply-To: <11bfdcs$1mvrv$1@dont-email.me>
Content-Language: en-GB, nb-NO