🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #761

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

#761
From: Tim Rentsch
Date: Sun, 25 Oct 2026 15:15
69 lines
2853 bytes
Richard Harnden <richard.nospam@gmail.invalid> writes:

> On 25/10/2026 04:24, Richard Harnden wrote:
>
>> On 23/10/2026 06: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
>>>
>>> the ai wrote me such variadic min
>>
>> I don't think that I'd bother with stdarg at all.
>>
>> Just use a string of ints ...
>>
>> int min(const int *n)
>> {
>>   int m = INT_MAX;
>>
>>   while ( *n )
>>   {
>>   if ( *n < m ) m = *n;
>>
>>   n++;
>>   }
>>
>>   return m;
>> }
>>
>>> int a=Min(4,10,3,7,-2); // -2
>>
>> int a = min((int[]){10,3,7,-2}));
>
> That has undefined behaviour - unfortunately, the kind that does what
> I expected :(
>
> You either need to pass the size ...
>
> int min(const int *n, size_t z)
> {
>     int m = INT_MAX;
>
>     for (size_t i=0; i<z; i++)
>         if ( n[i] < m ) m = n[i];
>
>     return m;
> }

If we are willing to treat all arguments as signed integers,
then it's fairly easy to give a solution:


   #include <stddef.h>
   #include <stdint.h>

   #define min(...) (                                                      \
     (min)( HOW_MANY( __VA_ARGS__ ), (intmax_t[]){ __VA_ARGS__ } )         \
   )

   #define HOW_MANY(...) (sizeof ((_Bool[]){ __VA_ARGS__ }) / sizeof (_Bool))

   intmax_t
   (min)( size_t n, intmax_t v[n] ){
     intmax_t r = INTMAX_MAX;
       for(  size_t k = 0;  k < n;  k++  ){
           r = v[k] < r ? v[k] : r;
       }
       return  r;
   }
Technical Details
Message-ID: <865wypolqd.fsf@linuxsc.com>
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> <11bk09r$3acgl$1@dont-email.me> <11bkn79$3he92$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: Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups: comp.lang.c
Subject: Re: variadic min and max, (any some and none, cat and more)
Date: Sun, 25 Oct 2026 08:15:06 -0700
Organization: A noiseless patient Spider
Lines: 69
Message-ID: <865wypolqd.fsf@linuxsc.com>
References: <11bes8b$1gncr$1@dont-email.me> <11bk09r$3acgl$1@dont-email.me> <11bkn79$3he92$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Date: Sun, 25 Oct 2026 15:15:09 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3907894"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1+phMwXOxtmh2Ec6A8TQiIkK88ye35SIsQ="; posting-host="6039183f6b171f0353470c147b7a97b0"
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:gZz0RI+wGesQwBoOq9bf41WfvRA=
	sha1:qRoNRVMKxWib/wBeKW789dSb03g= sha256:538lzoMozPGhFDQZRdO6/J0uSY+YaY/oC+7nFtLIoDQ=
	sha1:6Q/75TyJIfzJf8rWsSWNOETyKCU= sha256:YJ+OTp8IPlBYb+Hsjk4oRXWCnqFl6CSrdP1Pg5KoyE8=