Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #402587

Re: Dodging undefined behaviour in printf

From "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com>
Newsgroups comp.lang.c
Subject Re: Dodging undefined behaviour in printf
Date 2026-09-30 13:05 -0700
Organization A noiseless patient Spider
Message-ID <119jq22$igr0$1@dont-email.me> (permalink)
References <119bf9d$1fk39$1@dont-email.me>

Show all headers | View raw


On 9/27/2026 9:12 AM, highcrew wrote:
> 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.
> 
> 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?
> 
> I find it interesting that the problem is not a problem on architectures 
> where sizeof(unsigned short) == sizeof(unsigned int) -- if I will ever 
> find such a thing.
> 
> How about %hx then?
> 

Remember to cast to (void*) wrt a %p.

Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Dodging undefined behaviour in printf highcrew <high.crew3868@fastmail.com> - 2026-09-27 18:12 +0200
  Re: Dodging undefined behaviour in printf Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid> - 2026-09-28 01:47 +0800
  Re: Dodging undefined behaviour in printf Tim Rentsch <tr.17687@z991.linuxsc.com> - 2026-09-27 10:48 -0700
  Re: Dodging undefined behaviour in printf James Kuyper <jameskuyper@alumni.caltech.edu> - 2026-09-27 20:30 -0400
  Re: Dodging undefined behaviour in printf David Brown <david.brown@hesbynett.no> - 2026-09-28 09:50 +0200
  Re: Dodging undefined behaviour in printf "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2026-09-30 13:05 -0700

csiph-web