bug in lrint [was: FW: Printing long int in C program under cygwin64]

Eric Blake eblake@redhat.com
Thu May 25 19:25:00 GMT 2017


On 05/24/2017 06:36 PM, Steven Penny wrote:
> On Wed, 24 May 2017 07:33:27, Eric Blake wrote:
>> Buggy.  size_t should be printed with %zi, not %i (since size_t and int
>> are not necessarily the same type).
> 
> Aren’t both wrong? By definition %i is a signed integer, and size_t is
> unsigned.
> So %zu

Correct.  Newer gcc's -Wformat-signedness will flag the discrepency.

> or %llu would be more correct:

NO. Do NOT use %llu with size_t, because it is not portable to 32-bit
platforms.  That's WHY %zu exists.

> They all seem to do the job though:

Yes. On all modern platforms, you can freely mix signed and unsigned
integers and get correct (when the number is positive and does not
exceed the signed maximum) or at least sane results (2s complement
counterpart for all other values) regardless of which direction you mess
with incorrect signedness.  However, such behavior is technically not
required by the C standard, which is why gcc added -Wformat-signedness.

-- 
Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization:  qemu.org | libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 604 bytes
Desc: OpenPGP digital signature
URL: <http://cygwin.com/pipermail/cygwin/attachments/20170525/2a2c618e/attachment.sig>


More information about the Cygwin mailing list