> On 16 May 2025, at 14:29, Vincent Lefevre <vinc...@vinc17.net> wrote:
> 
> On 2025-05-16 13:29:09 +0200, Charlie Gordon wrote:
>> The correct portable way to print `int64_t` values is this:
>> ```
>> #include <stdio.h>
>> #include <stdint.h>
>> #include <inttypes.h>
>> 
>> int main(int argc, char **argv) {
>>   int64_t a =123, b = 234;
>>   printf("%" PRId64 " %" PRId64 "\n", a, b);
>>   return 0;
>> }
>> ```
> 
> This is just *a* correct portable way...
> 
> The user may keep int64_t, but cast to long long just for the printf:
> 
> [...]
>   int64_t a =123, b = 234;
>   printf("%lld %lld\n", (long long) a, (long long) b);
> [...]

Indeed this is possible and likely a simpler solution.

> This is portable because int64_t necessarily fits in a long long on
> any platform.

This is correct, but only since C23.  Previous versions of the C Standard would 
have allowed `long long`
to have a different representation from `int64_t` that would not support the 
whole set of values (eg: all 64-bit
values except -9223372036854775808) causing undefined behavior when this value 
is cast as `long long`.
A good corner case for a DS9K compiler :)

Chqrlie.

> 
> -- 
> Vincent Lefèvre <vinc...@vinc17.net> - Web: <https://www.vinc17.net/>
> 100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
> Work: CR INRIA - computer arithmetic / Pascaline project (LIP, ENS-Lyon)
> 
> _______________________________________________
> Tinycc-devel mailing list
> Tinycc-devel@nongnu.org
> https://lists.nongnu.org/mailman/listinfo/tinycc-devel


_______________________________________________
Tinycc-devel mailing list
Tinycc-devel@nongnu.org
https://lists.nongnu.org/mailman/listinfo/tinycc-devel

Reply via email to