Article View: comp.lang.c
Article #249Re: Dodging undefined behaviour in printf
From: Tim Rentsch
Date: Sun, 11 Oct 2026 23:05
Date: Sun, 11 Oct 2026 23:05
69 lines
4646 bytes
4646 bytes
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.
Technical Details
Message-ID:
<86v777j0t9.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-mailReferences:
<119bf9d$1fk39$1@dont-email.me> <11agtbo$30mh2$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: Dodging undefined behaviour in printf Date: Sun, 11 Oct 2026 16:05:06 -0700 Organization: A noiseless patient Spider Lines: 69 Message-ID: <86v777j0t9.fsf@linuxsc.com> References: <119bf9d$1fk39$1@dont-email.me> <11agtbo$30mh2$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Date: Sun, 11 Oct 2026 23:05:10 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3250881"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18dZd2g/tgEmiCQTh9a9dYNNWLpQH+QZWs="; posting-host="1098c1f1b6b0f5d684612506fbd55361" User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux) Cancel-Lock: sha1:x7zuxvnKVrs1xHOqWmHABJenNEE= sha1:vgQMI1n5REzx1KC/5buRZzrkgx0= sha256:BcU8w+3fsEgU+MXXoC+ed/WlA9O0+uQN3L7VDUraIVY= sha1:WB61Entf/yjCKhZZfNCG/i1DwuQ= sha256:iGxlYqdf3INLc535GNba2Ver28yixYpGx8/xZL4Na7M=