Article View: comp.lang.c
Article #234Re: Dodging undefined behaviour in printf
From: Andrey Tarasevich
Date: Sun, 11 Oct 2026 20:59
Date: Sun, 11 Oct 2026 20:59
33 lines
2587 bytes
2587 bytes
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, but there's no such footnote anymore. So, apparently, no such straightforward interchangeability is implied... 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 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. -- Best regards, Andrey
Technical Details
Message-ID:
<11agtbo$30mh2$1@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:
<119bf9d$1fk39$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: Andrey Tarasevich <noone@noone.net> Newsgroups: comp.lang.c Subject: Re: Dodging undefined behaviour in printf Date: Sun, 11 Oct 2026 13:59:34 -0700 Organization: A noiseless patient Spider Lines: 33 Message-ID: <11agtbo$30mh2$1@dont-email.me> References: <119bf9d$1fk39$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 11 Oct 2026 20:59:37 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3168802"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19F3ckL8JpL//CNLmiycWCM"; posting-host="4b64a6b55a0048c7998d82b9e7359498" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:Crg8921Yx9MKsNxTv8z6LzudCOk= sha256:vzaaKsabvkhE71ZNDxWbag1yZ/Z92tKRrRUhrw1myRA= sha1:KmLH5UxXbdIp3PkKSTMSLBcXv34= sha256:ETQpNACBQZOCc7g10iwmBxtCtzi7P/Vd2RbejyjECx8= In-Reply-To: <119bf9d$1fk39$1@dont-email.me> Content-Language: en-US