Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: printf and time_t Date: Fri, 06 Feb 2026 04:46:51 -0800 Organization: None to speak of Lines: 50 Message-ID: <87ecmy82f8.fsf@example.invalid> References: <10jfol6$2u6r8$1@news.xmission.com> <10jpi8h$15aea$1@dont-email.me> <20260109141859.00004f22@yahoo.com> <10jv3rb$15aea$2@dont-email.me> <20260111132015.000026ad@yahoo.com> <86zf6kkjw0.fsf@linuxsc.com> <20260111235104.00001463@yahoo.com> <86ms1pj0bc.fsf@linuxsc.com> <10ltjjt$1o4pk$1@dont-email.me> <865x8cio5y.fsf@linuxsc.com> <10lvt1s$2fu8f$1@dont-email.me> <10lvul0$2gps1$1@dont-email.me> <10m024m$2hqvi$1@dont-email.me> <10m091i$2kiff$1@dont-email.me> <10m0b0g$2l6li$1@dont-email.me> <10m1vl7$35irp$1@dont-email.me> <10m2kpb$3cm2p$1@dont-email.me> <10m374q$2d053$1@dont-email.me> <10m39sd$3ladr$1@dont-email.me> <87ikca8nj2.fsf@example.invalid> <10m4a1r$3ko8l$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Fri, 06 Feb 2026 12:46:52 +0000 (UTC) Injection-Info: dont-email.me; posting-host="44e384fb3fd0ff728a4bb0cd5ce7cfc9"; logging-data="180090"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+8Ajop7SCpLLDo60xWsK9q" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:hgK3tMFDyR6Z9Vp24AtonN24DNg= sha1:9ee8gfXfdrwBYONMYqSyeuGwc2k= Xref: csiph.com comp.lang.c:396623 Janis Papanagnou writes: > On 2026-02-06 06:10, Keith Thompson wrote: >> Bart writes: >> >>> If I compile this code with 'gcc -Wall -Wextra -Wpedantic': >>> >>> #include >>> >>> int main() { >>> int a = -1; >>> printf("%u", a); >>> } >>> >>> it says nothing. The program displays 4294967295 instead of -1. > > Yes. You instruct 'printf' with '%u' to interpret and display it > (the variable 'a') as unsigned. ('-1' is not an unsigned numeric > representation.) - I wonder what you are thinking here. > >> The behavior is unsurprising. The lack of a warning is very mildly >> inconvenient. > > Indeed unsurprising. And I even don't see any inconvenience given > that even an initialized declaration of 'unsigned a = -1;' is not > considered a problem in "C". I rather learned that to be a useful > code pattern when programming in "C". Sure, but the point is that the program has undefined behavior. If you define `unsigned a = -1;`, the value of the initializer is implicitly converted from int to unsigned, and the value of UINT_MAX is stored in `a`. That's well defined. Passing a argument of type int to printf with a "%u" format is well defined if and only if the value of the argument is within the ranges of both types (which is almost certainly equivalent to it being non-negative). This is strongly implied prior to C23, and guaranteed in C23. There is no implicit conversion; the int is treated as if it were of type unsigned int. In practice, it's almost certain to display the value of UINT_MAX unless the compiler goes out of its way to do something else. A warning would not be inappropriate -- and in fact clang version 21 does issue a warning with "-Weverything". (The warning refers to "-Wformat", which by itself doesn't trigger the warning; that might be a bug.) -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */