Article View: comp.lang.c
Article #722Re: variadic min and max, (any some and none, cat and more)
From: David Brown
Date: Fri, 23 Oct 2026 16:54
Date: Fri, 23 Oct 2026 16:54
30 lines
2415 bytes
2415 bytes
On 23/10/2026 18:40, Ike Naar wrote:
> On 2026-10-23, David Brown <david.brown@hesbynett.no> wrote:
>> 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.
>
> If the sentinel value were changed to INT_MAX the none-match requirement
> can be dropped (since INT_MAX is the identity element of the min operation
> it could safely appear in the argument list).
min(10, SENTINEL, 1) would still fail - it would stop when the sentinel
is reached, and return 10.
Technical Details
Message-ID:
<11bg3fa$1ojh8$5@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> <slrn11dn3gq.m3u.ike@iceland.freeshell.org>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 18:54:02 +0200 Organization: A noiseless patient Spider Lines: 30 Message-ID: <11bg3fa$1ojh8$5@dont-email.me> References: <11bes8b$1gncr$1@dont-email.me> <11bf84v$1jkb5$1@dont-email.me> <slrn11dn3gq.m3u.ike@iceland.freeshell.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Fri, 23 Oct 2026 16:54:03 +0000 (UTC) Injection-Info: dont-email.me; logging-data="1855016"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+YSVlXYPZSRLZNEPKDVRz0xUnujlsQFTg="; posting-host="7400fcdb32ce88f15c58e9bcb96ebcf6" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:KYxu8WrwfJbxSO1tOuPH0OQPa10= sha256:C5RpTcXL7UVSTrwiVK7qrq0tt8fmj+gaCA5LsJ30Q9I= sha1:9eXVPv6Mm5XkcebeGmIhgD010IY= sha256:EBSfvM68n/zl+N1UO4CCyIHmgt9OwJ8ECXS8/Ft/bys= In-Reply-To: <slrn11dn3gq.m3u.ike@iceland.freeshell.org> Content-Language: en-GB, nb-NO