🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #256

Re: Dodging undefined behaviour in printf

#256
From: Johann 'Myrkraverk' Oskarsson
Date: Mon, 12 Oct 2026 04:58
77 lines
5034 bytes
On 10/12/2026 7:05 AM, Tim Rentsch wrote:
> Andrey Tarasevich <noone@noone.net> writes:
> 
>> On Sun 9/27/2026 9:12 AM, highcrew wrote:
>>
>>> Well, it is is also true that the `int` value is not going to be
>>> negative for sure.
>>>
>>> Would this be a problem?  Am I supposed to explicitly cast x to
>>> (unsigned int) when passing it to printf?
>>
>> C89/90 explicitly stated that signed and unsigned versions of integer
>> types should use the same representation for the same values
>> (i.e. values representable in both).  And footnote 16 clarified that
>> this "meant to imply interchangeability as arguments to functions,
>> return values from functions, and members of unions".
>>
>> Newer version of the standard also makes similar representation
>> guarantees,
> 
> Starting in C99 there are more specific statements about the
> representations of integer types, and these descriptions establish
> that for values in the common subset of corresponding signed and
> unsigned types equal values have the same representation.
> 
>> but there's no such footnote anymore.  So, apparently, no
>> such straightforward interchangeability is implied...
> 
> All of C99, C11, and C23 have "meant to imply interchangeability"
> footnotes.  They appear in two places each in C99 and C11, and in
> three places in C23.  Just looking at them the wording appears
> identical in every case.
> 
>> One would expect `printf` to adhere to `va_arg`'s specification when
>> retrieving its variadic parameters.`va_arg` provides a special
>> guarantee for this specific situation.  However, I can't immediately
>> find any place in the standard that would require `printf` to behave
>> "as if" it uses `va_arg` (even for `vprintf`).
> 
> In C89/90, the description of va_arg requires the types (after
> default promotions have taken place) to be compatible.
> 
> In C99, the description of va_arg was changed so that using a signed
> type for the corresponding unsigned type, or vice versa, is okay, as
> long as the value is in the common subset of the two types.
> 
>> In real life one's normally expect `printf`/`vprintf` to behave "as
>> if" it uses `va_arg` internally, meaning that passing a non-negative
>> signed int` for an `unsigned int` format specifier should be fine.
> 
> I think most people, including myself, expected that the rules for
> va_arg would carry over to arguments of printf() and friends.
> Starting in C99 there is an explicit statement, both for arguments
> obtained by the use of va_arg and for arguments subject to the
> default argument promotions, that the statement show above applies.
> In C89/90 the descriptions of these cases said the types had to
> be compatible.  I think there was a Defect Report that clarified
> this but I haven't taken the time to verify that.  In any case
> starting in C99 both default argument promotions and use of va_arg
> provide for interchangeability of corresponding signed and unsigned
> types when the value is in the common subset (and also for void *
> and pointers to character types).
> 
> In C99 and C11 the rule for arguments of printf() and friends was
> stated somewhat fuzzily - arguments had to be of "the correct type"
> to avoid undefined behavior, but what "the correct type" meant was
> never stated.  Fortunately, in C23 the rule was changed to say that
> arguments of printf() and friends have to obey the same rule as the
> one for va_arg, so how printf() behaves now officially conforms to
> what everyone thought all along.

Until someone makes a non-conformant compiler, of course.
-- 
Johann | email: invalid -> com | http://www.myrkraverk.com/blog/
I'm not from the Internet, I just work there. | via Easynews.com
https://bsky.app/profile/myrkraverk.bsky.social | for ( ;; ) _:;
Federated at https://fed.brid.gy/bsky/myrkraverk.bsky.social
Technical Details
Message-ID: <mOZyS.10173$Ob1f.5588@fx05.ams4>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer04.iad!feed-me.highwinds-media.com!peer04.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!peer05.ams1!peer.ams1.xlned.com!news.xlned.com!fx05.ams4.POSTED!not-for-mail
References: <119bf9d$1fk39$1@dont-email.me> <11agtbo$30mh2$1@dont-email.me> <86v777j0t9.fsf@linuxsc.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer04.iad!feed-me.highwinds-media.com!peer04.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!peer05.ams1!peer.ams1.xlned.com!news.xlned.com!fx05.ams4.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: Dodging undefined behaviour in printf
Newsgroups: comp.lang.c
References: <119bf9d$1fk39$1@dont-email.me> <11agtbo$30mh2$1@dont-email.me>
 <86v777j0t9.fsf@linuxsc.com>
Content-Language: en-US
From: Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid>
Organization: Watcom Pro Ltd.
In-Reply-To: <86v777j0t9.fsf@linuxsc.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 77
Message-ID: <mOZyS.10173$Ob1f.5588@fx05.ams4>
X-Complaints-To: abuse@easynews.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Mon, 12 Oct 2026 12:58:57 +0800
X-Received-Bytes: 4849