Article View: comp.lang.c
Article #720Re: variadic min and max, (any some and none, cat and more)
From: Ike Naar
Date: Fri, 23 Oct 2026 16:40
Date: Fri, 23 Oct 2026 16:40
25 lines
2007 bytes
2007 bytes
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).
Technical Details
Message-ID:
<slrn11dn3gq.m3u.ike@iceland.freeshell.org>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: Ike Naar <ike@sdf.org> Newsgroups: comp.lang.c Subject: Re: variadic min and max, (any some and none, cat and more) Date: Fri, 23 Oct 2026 16:40:58 -0000 (UTC) Organization: A noiseless patient Spider Lines: 25 Message-ID: <slrn11dn3gq.m3u.ike@iceland.freeshell.org> References: <11bes8b$1gncr$1@dont-email.me> <11bf84v$1jkb5$1@dont-email.me> Injection-Date: Fri, 23 Oct 2026 16:40:59 +0000 (UTC) Injection-Info: dont-email.me; logging-data="2073540"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18ovMixi7PzT2ea3sAHIfB9"; posting-host="0b661e8b0a569d4fc4ff0910fe9e12ef" User-Agent: slrn/1.0.3 (Patched for libcanlock3) (NetBSD) Cancel-Lock: sha1:XEIjtmtaVHY46grgGvcUIH9nVoA= sha256:STJphTclJgEhvcw06HX1D93ukyN7H9FHtAccrhzNO0A= sha1:IUi42/00oJ7MQa7AgD+JEsej50o= sha256:qztvBPMGGmEccZqKKJUv0XhhqGECnIZBUdedjUE9YTs=