🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.c
Article #255

Re: Dodging undefined behaviour in printf

#255
From: David Brown
Date: Mon, 12 Oct 2026 04:38
87 lines
5001 bytes
On 11/10/2026 21:45, MitchAlsup wrote:
> 
> highcrew <high.crew3868@fastmail.com> posted:
> 
>> Dear c.l.c.
>>
>> I'm trying to refine my knowledge on the topic of type promotions, and
>> now I know how promotion is implied when passing arguments to a function
>> whose prototype is missing argument definition, or to functions having
>> variadic argument list.
>>
>> 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 is simply incorrect.

If you have a value of an integer type with rank less than that of "int" 
(and "short", signed or unsigned, always has lower rank than "int" 
regardless of the actual bit-widths) then whenever an "int" or "unsigned 
int" could be used, the smaller type is "promoted".  If an "int" can 
represent all values of the original type, the promotion is to "int" - 
otherwise it is to "unsigned int".  Integer promotion is never to "long" 
types (signed or unsigned).  These rules are in the C standards, and are 
not dependent on sizes.

So if an "int" can represent all the values of "unsigned short", then 
"x" is promoted to "int".  This is the case on most 32-bit and 64-bit 
systems (I think early Cray machines are the only known exceptions, as 
"short", "int" and "long" types were all 64-bit).  On systems with 
16-bit "int", "unsigned short" values will be promoted to "unsigned int" 
- changing type despite remaining the same size.

The sign of the /value/ will not change during integer promotion - the 
signedness of the /type/ typically does (if the original short or char 
was an unsigned type) on 32-bit and 64-bit systems.

> 
>> Well, it is is also true that the `int` value is not going to be
>> negative for sure.
> 
> Unclear !! 

It is entirely clear - "the integer promotions preserve value including 
sign."  (That is a direct quotation from the standard.)

> 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).
> 

Instruction choice is irrelevant - other than that the compiler must 
choose instructions that give the correct standards-mandated behaviour.

> On the called side, the arguments are going to be stored in memory in a
> linear vector (varargs) so they can be extracted one after another such
> that the called side does not need to know how many there are. One gets
> a pointer to the linear vector with vastart.
>   

Some ABIs use register operands for variadic functions too.  But again, 
the details are irrelevant.

>> Would this be a problem? Am I supposed to explicitly cast x to (unsigned
>> int) when passing it to printf?
> 
> Do not lie to your variable argument functions.

That is good advice.  In this particular case, the OP is not lying to 
his variadic function.  For values that are in the intersection of the 
ranges of "int" and "unsigned int", the signedness of the int type does 
not matter for variadic arguments.  (There are many other circumstances 
in C where it does matter, but not for variadic arguments.)

Technical Details
Message-ID: <11aho7k$38ir0$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-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!.POSTED!not-for-mail
From: David Brown <david.brown@hesbynett.no>
Newsgroups: comp.lang.c
Subject: Re: Dodging undefined behaviour in printf
Date: Mon, 12 Oct 2026 06:38:12 +0200
Organization: A noiseless patient Spider
Lines: 87
Message-ID: <11aho7k$38ir0$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; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 12 Oct 2026 04:38:13 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3427168"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX18qUb0C19nUZhSfPZaAJOvCvAAV3pGhMQM="; posting-host="711f10de653bed0662d2460037681553"
User-Agent: Mozilla Thunderbird
Cancel-Lock: sha1:Wcwt0s6tCxa7QYcy+BGUbAdMSu4= sha256:zi9ej5aYyUF+sM5/mDCoxBMNICNaGWeOWzhtL4D1r7E=
	sha1:cYAcAGrTLmjQ0M++A9domu8RTTw= sha256:GGmu46XRwHKc3JzZ3m6B26jzfwayhH66Ni4inDiMJvQ=
Content-Language: en-GB, nb-NO
In-Reply-To: <1791747948-5857@newsgrouper.org>