Article View: comp.lang.c
Article #714Re: variadic min and max, (any some and none, cat and more)
From: David Brown
Date: Fri, 23 Oct 2026 13:18
Date: Fri, 23 Oct 2026 13:18
123 lines
6543 bytes
6543 bytes
On 23/10/2026 14:53, bartc wrote:
> 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.
If you want to argue about ABIs and APIs, go back to the thread where
you were doing that already. You don't have to bring up every topic
that frustrates you or irritates you into every thread!
>
>
>>> 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.)
No, it is no longer a sentinel - it will be used directly, so the value
printed will be 0. (It was you that used 0 as a sentinel - in my
earlier example, I used INT_MIN as the sentinel as that is far less
likely to conflict with useful values. And it was hidden in the macros.
>
>> 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.
(I hope that in the future, you remember that I called your solution,
albeit with a macro for a slightly nicer interface, "not bad". And
remember also that I am Scottish - "not bad" is as complimentary as we get!)
>
>>> 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.
>
I agree. Min is useful sometimes, but it's not very common. And the
great majority of uses are probably for two values. I expect N-way min
turns up more in examples for discussions like this than it does in real
code.
> (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.)
>
Higher level functions like "reduce", or variadic template functions,
are useful features in a language. But would be big features to add to
a relatively small language, and I don't see them being appropriate for
C. Big languages, like C++ and Python, have good solutions for this (as
do some smaller languages where the emphasis is different from C).
Technical Details
Message-ID:
<11bfmqq$1ojh8$3@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> <11bflcp$1q9v8$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 15:18:18 +0200 Organization: A noiseless patient Spider Lines: 123 Message-ID: <11bfmqq$1ojh8$3@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> <11bflcp$1q9v8$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 13:18:19 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1855016"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ckpU7lLcyCgXjgTgnJYGmRGPX08WlS9E="; posting-host="7400fcdb32ce88f15c58e9bcb96ebcf6" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:zzMBexM9zdlxyCNYvfqVZzv2HIo= sha256:uFAm3t698efh6/QQD/IyBlb+/UChNIaeHrJ3zNk5mhQ= sha1:2OZj7IvcZytkD/x7tEXq5lZ/2ek= sha256:1CAbBQX5YPU0As2Qumvz6oxxE87zJDM3Z5j/cD0K8Ts= Content-Language: en-GB, nb-NO In-Reply-To: <11bflcp$1q9v8$1@dont-email.me>