Path: csiph.com!weretis.net!feeder8.news.weretis.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Tim Rentsch
Newsgroups: comp.lang.c
Subject: Re: Dymamic arrays: memory management and naming
Date: Mon, 25 Sep 2023 19:00:50 -0700
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <86sf71itgt.fsf@linuxsc.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc> <86ediwmk8b.fsf@linuxsc.com> <20230919011018.d9ed14da6e842291d47b69d7@gmail.moc> <86jzsmkqyu.fsf@linuxsc.com> <20230924003915.51bb21339ab1c551eba59162@gmail.moc> <20230924081911.947@kylheku.com> <20230925155845.139@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="52bf0d5ee5119c0cfc0137a030202cf0"; logging-data="2436956"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18mWa8MJT47Wu/BkFnmgJnTwsUWGSWYkbU="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:VKulYtjDJlPlMO1TKsg/reUKhQM= sha1:t7jZfm0Vc0nys3DF6ZmDU2EIzAk=
Xref: csiph.com comp.lang.c:176410
Kaz Kylheku <864-117-4973@kylheku.com> writes:
> On 2023-09-24, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>
>> h only makes sense if you're passing a signed argument and want it
>> printed as unsigned. Say we have 16 bit short and 32 bit int.
>> Then "%u"/(short) -1 will print as 429496725, whereas "%hu"/(short) -1
>> will obtain the desired 65535.
>
> There is something I had neglected to mention: the use of h for portability
>
> If we have "%u" paired with an unsigned short, argument we have
> a mismatch on a large number of platforms, where unsigned short promotes
> to int. The %u specifier wants unsigned int.
A %u conversion specification is always okay for an unsigned
short argument, regardless of whether the argument promotes to
int or to unsigned int. Reading an integer-typed variadic
argument with the corresponding type of the other signedness
is allowed when the argument value is representable in both
types, which it always will be in this case even if unsigned
short promotes to int.