Article View: comp.lang.c
Article #709Re: variadic min and max, (any some and none, cat and more)
From: bartc
Date: Fri, 23 Oct 2026 12:53
Date: Fri, 23 Oct 2026 12:53
94 lines
4997 bytes
4997 bytes
On 23/10/2026 12:49, David Brown wrote:
> 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.
But it is little to do with APIs other than those tend to get mixed up.
>> 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));
(Is that 0 still a sentinel here? If so it would anyway be better as as
INT_MAX or some nicer alias.)
> Not a bad solution, when a single fixed type will do.
OK. I'm don't know how to do solutions with macros. It is a harder
language than C.
>> 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.)
The problem I have with this is use-cases.
I feel that a special solution for N-way min(), where N is a small
compile-time value, is as useful as an N-way solution for 'add'.
It can happen, but in the case of 'add', you just write x + y + z, you
don't need +(x, y, z).
So for min, it would be min(x, min(y, z)) which is not too onerous.
(But if 'min' was an operator that can be used in-fix style as well as
function-like [I do this], then this would also be x min y min z.
Of course, in a higher level language you can have a solution based on
using 'reduce(min, x)' where 'min' works on two operands, and x is a
list. But this would be a poor fit here.)
Technical Details
Message-ID:
<11bflcp$1q9v8$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> <11bfdcs$1mvrv$1@dont-email.me> <11bfhl3$1ojh8$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 13:53:45 +0100 Organization: A noiseless patient Spider Lines: 94 Message-ID: <11bflcp$1q9v8$1@dont-email.me> References: <11bes8b$1gncr$1@dont-email.me> <11bf84v$1jkb5$1@dont-email.me> <11bfdcs$1mvrv$1@dont-email.me> <11bfhl3$1ojh8$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 12:53:46 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1910760"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/gFL7hzzU1kcY4UMB6oXse"; posting-host="df1406970d80d921c8a9b174013cabf0" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:AnXq+4iXZWpsrv6UxZ7z6XOinjU= sha256:L4gmeREwwHny/ZZuqSSeB4UaS+iLUIzc6fPKEuT1dIQ= sha1:KZSkdGl3eV+hHBwtuxHXRjmN1Xc= sha256:a6/27jSdhx7coOzy9oGeswxIKr9r4Bwnf87PwFsYpy8= Content-Language: en-GB In-Reply-To: <11bfhl3$1ojh8$1@dont-email.me>