Article View: comp.lang.c
Article #268Re: Dodging undefined behaviour in printf
From: James Kuyper
Date: Mon, 12 Oct 2026 15:32
Date: Mon, 12 Oct 2026 15:32
40 lines
3137 bytes
3137 bytes
On 2026-10-11 15:45, MitchAlsup wrote:
>
> highcrew <high.crew3868@fastmail.com> posted:
...>> unsigned short x = value;
>> printf("%x", x);
...> On a 64-bit machine, x gets promoted to unsigned long int.
> On a 32-bit machine, x gets promoted to unsigned int.
>
> signedness is not changed during promotion.
"if an int can represent all values of the original type (as restricted
by the width, for a bit-field), the value is converted to an int;50)
otherwise, it is converted to an unsigned int. These are called the
integer promotions. All other types are unchanged by the integer
promotions." (6.3.2.1p2)
Note that the first appearance of "integer promotions" is in italics, an
ISO convention indicating that the sentence in which it appears is the
official definition of that term.
Thus, if UCHAR_MAX is less than INT_MAX, unsigned char will promote to
int. If USHRT_MAX < INT_MAX, unsigned short will also promote to int.
Note that it is always the range of representable values that matters,
not the size of the type; integer types are allowed to have padding
bits, so the size is not sufficient to determine the range.
The integer promotions never convert any other type to a long type.
>> Well, it is is also true that the `int` value is not going to be
>> negative for sure.
>
> Unclear !! Depending on ABI, the compiler might LD x using a LDUH instruction
> and by the time it gets to a register is "has already been promoted". As
> most ABIs pas at least a few arguments in registers, the LDUH is often all
> that gets done on the calling side. {However, if x is loaded as a short
> and then promoted, strange things may happen when the HoB of x is set (1).
"The integer promotions preserve value including sign." (6.3.2.1p4). Any
implementation that does what you say would be non-conforming.
Technical Details
Message-ID:
<11aiuio$3njr9$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> <1791747948-5857@newsgrouper.org>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: James Kuyper <jameskuyper@alumni.caltech.edu> Newsgroups: comp.lang.c Subject: Re: Dodging undefined behaviour in printf Date: Mon, 12 Oct 2026 11:32:38 -0400 Organization: A noiseless patient Spider Lines: 40 Message-ID: <11aiuio$3njr9$1@dont-email.me> References: <119bf9d$1fk39$1@dont-email.me> <1791747948-5857@newsgrouper.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Injection-Date: Mon, 12 Oct 2026 15:32:40 +0000 (UTC) Injection-Info: dont-email.me; logging-data="3919721"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19jbtdf7tbMK81D0SzzfyqGcKBsWZ2k6vY="; posting-host="e4543ade477de3820d7a39e4e6de828c" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:5/0DI8yeNOzK+x9uv/lAPSkSCpw= sha256:cw5sxDmPZYTXjcmBCRuzt9+sqcNnHYewFu41qZ8Uh9w= sha1:/RaT+n2T6Mq81LG/B25BU02Wqw4= sha256:oawb5H049jKEO+6e+NL0mygYH9UEAZDm9qFnTfWnAxU= In-Reply-To: <1791747948-5857@newsgrouper.org> Content-Language: en-US