🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #248

Re: Dodging undefined behaviour in printf

#248
From: Keith Thompson
Date: Sun, 11 Oct 2026 23:01
36 lines
2511 bytes
MitchAlsup <user5857@newsgrouper.org.invalid> writes:
> highcrew <high.crew3868@fastmail.com> posted:
[...]
>> So I was wondering about the following:
>> 
>> 	unsigned short x = value;
>> 	printf("%x", x);
>> 
>> Assuming that sizeof(unsigned short) < sizeof(int), I would guess that x 
>> is promoted to signed int, which is however the wrong type, as it should 
>> be unsigned int.
>
> 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.

That's incorrect.

An operand of any integer type narrower than signed/unsigned int is
converted to (signed) int if int can represent all values of the
original type, or to unsigned int otherwise.

The integer promotions never convert a value to unsigned long int.

On typical modern 32-bit and 64-bit implementations, short is 16
bits and int is 32 bits.  unsigned short on such implementations
is always promoted to signed int.  unsigned short would only be
promoted to unsigned int in implementations where short and int are
the same size (it's not *quite* that simple).  Such implementations

[...]

-- 
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Technical Details
Message-ID: <11ah4fs$329bt$4@kst.eternal-september.org>
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!kst.eternal-september.org!.POSTED!not-for-mail
References: <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!kst.eternal-september.org!.POSTED!not-for-mail
From: Keith Thompson <Keith.S.Thompson+u@gmail.com>
Newsgroups: comp.lang.c
Subject: Re: Dodging undefined behaviour in printf
Date: Sun, 11 Oct 2026 16:01:15 -0700
Organization: None to speak of
Lines: 36
Message-ID: <11ah4fs$329bt$4@kst.eternal-september.org>
References: <119bf9d$1fk39$1@dont-email.me> <1791747948-5857@newsgrouper.org>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Date: Sun, 11 Oct 2026 23:01:16 +0000 (UTC)
Injection-Info: kst.eternal-september.org;
	logging-data="3220861"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19XSpSE5xwetbOpurdS4mO5"; posting-host="50698cca2518b68a86663845e71c144a"
User-Agent: Gnus/5.13 (Gnus v5.13)
Cancel-Lock: sha1:YWWqj2i7JjR92oO+iT+QyJi15d0= sha256:kxhaG8fi6ZrC0PKjGHSAkTFVoV+rqGXx9e0rtaWuG6c=
	sha1:2DgHqmRUoFd2p9aZ+4eD21/Ej9I= sha256:E612C+JGAxHBjCnmsm17pzB94lGjoDWQeqFQ8orlnUE=